Skip to content

Commit dde2f55

Browse files
author
monsoon
committed
commit first
0 parents  commit dde2f55

33 files changed

+954
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
���߳����ģʽѧϰ

src/cn/xfj/futurepatten/Data.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cn.xfj.futurepatten;
2+
3+
public interface Data {
4+
5+
public abstract String getContent() throws Exception;
6+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.xfj.futurepatten;
2+
3+
public class FutureData implements Data{
4+
5+
private RealData realData = null;
6+
7+
private boolean ready = false;
8+
9+
private Exception exception = null;
10+
11+
public synchronized void setRealData(RealData realData){
12+
if(ready || realData == null){
13+
return;
14+
}
15+
this.realData = realData;
16+
ready = true;
17+
this.notifyAll();
18+
}
19+
20+
@Override
21+
public synchronized String getContent() throws Exception{
22+
while(!ready){
23+
try {
24+
System.out.println("执行wait");
25+
this.wait();
26+
System.out.println("退出wait");
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
if(exception != null){
32+
System.out.println("抛出异常");
33+
throw this.exception;
34+
}
35+
return this.realData.getContent();
36+
}
37+
38+
public synchronized void setException(Exception e){
39+
if(null == e || ready){
40+
return;
41+
}
42+
this.exception = e;
43+
ready = true;
44+
this.notifyAll();
45+
}
46+
47+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cn.xfj.futurepatten;
2+
3+
public class FutureMain {
4+
5+
public static void main(String[] args) {
6+
try {
7+
System.out.println("Main Start");
8+
Data future1 = Host.request(-1, 'A');
9+
//Data future2 = Host.request(20, 'B');
10+
//Data future3 = Host.request(30, 'C');
11+
try {
12+
Thread.sleep(1);
13+
} catch (InterruptedException e) {
14+
e.printStackTrace();
15+
}
16+
// System.out.println(future2.getContent());
17+
System.out.println(future1.getContent());
18+
// System.out.println(future3.getContent());
19+
} catch (Exception e) {
20+
e.printStackTrace();
21+
System.out.println("得到异常");
22+
}
23+
System.out.println("Main stop");
24+
}
25+
26+
}

src/cn/xfj/futurepatten/Host.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.xfj.futurepatten;
2+
3+
public class Host {
4+
5+
public static Data request(final int count,final char c){
6+
final FutureData futureData = new FutureData();
7+
System.out.println("Request start");
8+
new Thread(){
9+
public void run(){
10+
System.out.println("subThread start");
11+
try {
12+
RealData realData = new RealData(count,c);
13+
futureData.setRealData(realData);
14+
} catch (Exception e) {
15+
futureData.setException(e);;
16+
}
17+
System.out.println("subThread end");
18+
}
19+
}.start();
20+
System.out.println("Request end");
21+
return futureData;
22+
23+
}
24+
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package cn.xfj.futurepatten;
2+
3+
public class RealData implements Data{
4+
5+
private String content = null;
6+
7+
private Exception exception = null;
8+
9+
public Exception getException(){
10+
return this.exception;
11+
}
12+
public RealData(int count,char c){
13+
14+
char[] charBuffer = new char[count];
15+
for(int i = 0; i < count;i++){
16+
charBuffer[i] = c;
17+
}
18+
try {
19+
Thread.sleep(100);
20+
} catch (InterruptedException e) {
21+
e.printStackTrace();
22+
}
23+
this.content = new String(charBuffer);
24+
}
25+
@Override
26+
public String getContent() {
27+
return this.content;
28+
}
29+
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.xfj.miniserver;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
7+
public class MiniServer {
8+
9+
private final int serverPort;
10+
11+
public MiniServer(int serverPort){
12+
this.serverPort = serverPort;
13+
}
14+
15+
public void execute() throws IOException{
16+
ServerSocket serverSocket = new ServerSocket(serverPort);
17+
System.out.println("Listen on prot " + serverPort);
18+
try{
19+
while(true){
20+
System.out.println("Accepting ...");
21+
Socket socketClient = serverSocket.accept();
22+
System.out.println("connected to " + socketClient);
23+
new Thread(){
24+
@Override
25+
public void run(){
26+
Service.service(socketClient);
27+
}
28+
}.start();
29+
}
30+
}finally{
31+
serverSocket.close();
32+
}
33+
}
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.xfj.miniserver;
2+
3+
import java.io.IOException;
4+
5+
public class MiniServiceMain {
6+
7+
public static void main(String[] args) {
8+
MiniServer server = new MiniServer(8888);
9+
try {
10+
server.execute();
11+
} catch (IOException e) {
12+
e.printStackTrace();
13+
}
14+
}
15+
}

src/cn/xfj/miniserver/Service.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cn.xfj.miniserver;
2+
3+
import java.io.DataOutputStream;
4+
import java.io.IOException;
5+
import java.net.Socket;
6+
7+
public class Service {
8+
9+
public static void service(Socket socket){
10+
System.out.println(Thread.currentThread().getName() + ": Service.service (" + socket + ") Begin");
11+
try {
12+
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
13+
out.writeBytes("HTTP/1.0 200\r\n");
14+
out.writeBytes("Content-type: text/html\r\n");
15+
out.writeBytes("\r\n");
16+
out.writeBytes("<html><head><title>Countdown</title></head><body>");
17+
out.writeBytes("<h1>Count start!</h1>");
18+
for(int i = 10;i > 0;i--){
19+
System.out.println(Thread.currentThread().getName() + "Count i =" + i);
20+
out.writeBytes("<br>"+i+"<br>");
21+
out.flush();
22+
try {
23+
Thread.sleep(1000);
24+
} catch (InterruptedException e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
} catch (IOException e) {
29+
e.printStackTrace();
30+
}finally{
31+
try {
32+
socket.close();
33+
} catch (IOException e) {
34+
e.printStackTrace();
35+
}
36+
System.out.println(Thread.currentThread().getName() + "Service.service (" + socket + ") END");
37+
}
38+
39+
}
40+
}

src/cn/xfj/mythread/Gate.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cn.xfj.mythread;
2+
3+
public class Gate {
4+
private String name ;
5+
private String address;
6+
private int count = 0;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public String getAddress() {
17+
return address;
18+
}
19+
20+
public void setAddress(String address) {
21+
this.address = address;
22+
}
23+
24+
public int getCount() {
25+
return count;
26+
}
27+
28+
public void setCount(int count) {
29+
this.count = count;
30+
}
31+
32+
private void addCount(){
33+
this.count++;
34+
}
35+
36+
private void check(){
37+
if(name.charAt(0) != address.charAt(0)){
38+
System.out.println(count +" ERROR in thread " + name +" " + address);
39+
}
40+
}
41+
42+
public synchronized void pass(String name,String address){
43+
this.name = name;
44+
this.address = address;
45+
this.addCount();
46+
this.check();
47+
}
48+
}

0 commit comments

Comments
 (0)