Skip to content

Commit e76d1de

Browse files
committed
线程相关的设计模式
1 parent 3bc8a8d commit e76d1de

File tree

10 files changed

+313
-0
lines changed

10 files changed

+313
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package DesignPattern.future;
2+
3+
import jdk2.ThreadPool;
4+
5+
/**
6+
* Created by chenyang on 2017/3/25.
7+
*/
8+
public class Client {
9+
public Data request(final String queryStr){
10+
final FutureData future=new FutureData();
11+
new Thread(){
12+
public void run(){
13+
RealData realData=new RealData(queryStr);
14+
future.setRealData(realData);
15+
}
16+
}.start();
17+
return future;
18+
}
19+
20+
public static void main(String[] args) {
21+
Client client=new Client();
22+
23+
Data data=client.request("name");
24+
System.out.println("请求完毕");
25+
try {
26+
Thread.sleep(50);
27+
}catch (InterruptedException e){
28+
29+
}
30+
System.out.println("数据="+data.getResult());
31+
}
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package DesignPattern.future;
2+
3+
/**
4+
* Created by chenyang on 2017/3/25.
5+
*/
6+
public interface Data {
7+
public String getResult();
8+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package DesignPattern.future;
2+
3+
/**
4+
* Created by chenyang on 2017/3/25.
5+
*/
6+
public class FutureData implements Data {
7+
protected RealData realData=null;
8+
protected boolean isReady=false;
9+
10+
public synchronized void setRealData(RealData realdata){
11+
if(isReady){
12+
return;
13+
}
14+
this.realData=realdata;
15+
isReady=true;
16+
notifyAll();
17+
}
18+
19+
@Override
20+
public synchronized String getResult() {
21+
while (!isReady){
22+
try {
23+
wait();
24+
}catch (InterruptedException e){
25+
26+
}
27+
}
28+
return realData.result;
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package DesignPattern.future;
2+
3+
/**
4+
* Created by chenyang on 2017/3/25.
5+
*/
6+
public class RealData implements Data {
7+
protected final String result;
8+
9+
public RealData(String para) {
10+
//RealData的构造可能很慢,需要用户等待很久,这里使用sleep模式。
11+
StringBuilder sb=new StringBuilder();
12+
for(int i=0;i<10;i++){
13+
sb.append(para);
14+
try {
15+
//这里使用sleep,代替一个很慢的操作过程。
16+
Thread.sleep(100);
17+
}catch (InterruptedException e){
18+
19+
}
20+
}
21+
result=sb.toString();
22+
}
23+
24+
@Override
25+
public String getResult() {
26+
return result;
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package DesignPattern.jdkFuture;
2+
3+
import java.util.concurrent.ExecutionException;
4+
import java.util.concurrent.ExecutorService;
5+
import java.util.concurrent.Executors;
6+
import java.util.concurrent.FutureTask;
7+
8+
/**
9+
* Created by chenyang on 2017/3/26.
10+
*/
11+
public class FutureMain {
12+
public static void main(String[] args) throws Exception{
13+
FutureTask<String> future=new FutureTask<String>(new RealData("a"));
14+
ExecutorService executor= Executors.newFixedThreadPool(1);
15+
16+
executor.submit(future);
17+
18+
System.out.println("请求完毕");
19+
20+
try {
21+
Thread.sleep(2000);
22+
}catch (InterruptedException ex){
23+
24+
}
25+
26+
System.out.println("数据="+future.get());
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package DesignPattern.jdkFuture;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.Future;
6+
import java.util.concurrent.FutureTask;
7+
8+
/**
9+
* Created by chenyang on 2017/3/26.
10+
*/
11+
public class FutureMain2 {
12+
public static void main(String[] args) throws Exception{
13+
ExecutorService executor= Executors.newFixedThreadPool(1);
14+
Future<String> future=executor.submit(new RealData("a"));
15+
16+
System.out.println("请求完毕");
17+
System.out.println( future.isDone());
18+
try {
19+
Thread.sleep(2000);
20+
}catch (InterruptedException ex){
21+
22+
}
23+
System.out.println("数据="+future.get());
24+
System.out.println(future.isDone());
25+
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package DesignPattern.jdkFuture;
2+
3+
import java.util.concurrent.Callable;
4+
5+
/**
6+
* Created by chenyang on 2017/3/26.
7+
*/
8+
public class RealData implements Callable<String> {
9+
private String para;
10+
11+
public RealData(String para) {
12+
this.para = para;
13+
}
14+
15+
@Override
16+
public String call() throws Exception {
17+
StringBuilder sb=new StringBuilder();
18+
for(int i=0;i<10;i++){
19+
sb.append(para);
20+
try {
21+
Thread.sleep(100);
22+
}catch (InterruptedException e){
23+
24+
}
25+
}
26+
return sb.toString();
27+
}
28+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package DesignPattern.socket;
2+
3+
import java.io.*;
4+
import java.net.InetSocketAddress;
5+
import java.net.Socket;
6+
import java.net.UnknownHostException;
7+
8+
/**
9+
* Created by chenyang on 2017/3/26.
10+
*/
11+
public class MultiThreadEchoClient {
12+
13+
public static void main(String[] args) {
14+
Socket client=null;
15+
PrintWriter writer=null;
16+
BufferedReader reader=null;
17+
try {
18+
client=new Socket();
19+
client.connect(new InetSocketAddress("localhost",8000));
20+
writer=new PrintWriter(client.getOutputStream(),true);
21+
writer.println("hello"+System.currentTimeMillis());
22+
writer.flush();
23+
System.out.println(System.currentTimeMillis());
24+
reader=new BufferedReader(new InputStreamReader(client.getInputStream()));
25+
System.out.println(System.currentTimeMillis()+"from server:"+reader.readLine());
26+
}catch (UnknownHostException ex){
27+
ex.printStackTrace();
28+
}catch (IOException e){
29+
e.printStackTrace();
30+
} finally {
31+
if(writer!=null){
32+
writer.close();
33+
}
34+
if(reader!=null){
35+
try {
36+
reader.close();
37+
}catch (IOException ex){
38+
ex.printStackTrace();
39+
}
40+
41+
}
42+
if(client!=null){
43+
try {
44+
client.close();
45+
}catch (IOException ex){
46+
ex.printStackTrace();
47+
}
48+
}
49+
}
50+
}
51+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package DesignPattern.socket;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.PrintWriter;
7+
import java.net.ServerSocket;
8+
import java.net.Socket;
9+
import java.util.concurrent.ExecutorService;
10+
import java.util.concurrent.Executors;
11+
12+
/**
13+
* 常规的socket服务端,服务器端采用一个线程接受一个客户端来处理。
14+
* Created by chenyang on 2017/3/26.
15+
*/
16+
public class MultiThreadEchoServer {
17+
private static ExecutorService tp= Executors.newCachedThreadPool();
18+
static class HandleMsg implements Runnable{
19+
Socket clientSocket;
20+
21+
public HandleMsg(Socket clientSocket) {
22+
this.clientSocket = clientSocket;
23+
}
24+
25+
@Override
26+
public void run() {
27+
BufferedReader is=null;
28+
PrintWriter os=null;
29+
try {
30+
is=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
31+
os=new PrintWriter(clientSocket.getOutputStream(),true);
32+
//从InputStream当中读取客户端所发送的数据
33+
String inputLine=null;
34+
long b=System.currentTimeMillis();
35+
while ((inputLine=is.readLine())!=null){
36+
os.println(inputLine);
37+
}
38+
long e=System.currentTimeMillis();
39+
System.out.println("spend:"+(e-b)+"ms");
40+
}catch (IOException e){
41+
e.printStackTrace();
42+
}finally {
43+
try {
44+
if(is!=null) is.close();
45+
if(os!=null) os.close();
46+
clientSocket.close();
47+
}catch (IOException ex){
48+
ex.printStackTrace();
49+
}
50+
}
51+
}
52+
}
53+
54+
public static void main(String[] args) {
55+
ServerSocket echoServer=null;
56+
Socket clientSocket=null;
57+
try {
58+
echoServer=new ServerSocket(8000);
59+
}catch (IOException e){
60+
System.out.println(e);
61+
}
62+
while (true){
63+
try {
64+
clientSocket =echoServer.accept();
65+
System.out.println(clientSocket.getRemoteSocketAddress()+" connect!"+System.currentTimeMillis());
66+
tp.execute(new HandleMsg(clientSocket));
67+
}catch (IOException e){
68+
System.out.println(e);
69+
}
70+
}
71+
}
72+
}

HighConcurreny/src/main/java/ParallelBasic/SimpleWN.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ public class SimpleWN {
1616
public static class T1 extends Thread{
1717
@Override
1818
public void run() {
19+
//加上这句虽然造成T1一直等待,但是其他线程可以调用同步方法,因为T1已经释放了锁资源
20+
// try {
21+
//
22+
//
23+
// sleep(100);
24+
// }catch (InterruptedException ex){
25+
//
26+
// }
27+
1928
synchronized (object){
2029
System.out.println(System.currentTimeMillis()+":T1 start!");
2130
try {

0 commit comments

Comments
 (0)