Skip to content

Commit df200b0

Browse files
committed
java-multithread communication更新至example16,本包告一段落
1 parent 7ebbda2 commit df200b0

35 files changed

+1241
-1
lines changed

blogs/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
# 博客目录
1+
# 博客
2+
3+
如果你只是单纯要阅读的话,建议移步CSDN或者oschina上观看,访问速度快很多:
4+
5+
>* CSDN:[我的java&javaweb学习笔记(汇总)](http://blog.csdn.net/h3243212/article/details/50659471)
6+
>* oschina:[我的java&javaweb学习笔记(汇总)](http://my.oschina.net/brianway/blog/614355)
7+
8+
9+
# 目录
210

311
- [javase](/blogs/javase)
412
- [java基础巩固笔记(1)-反射.md](/blogs/javase/java基础巩固笔记(1)-反射.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.brianway.learning.java.base;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* 待理解。
9+
* 应该是考装箱和拆箱
10+
*/
11+
public class Boxing {
12+
public static void main(String[] args) {
13+
Integer a = 1;
14+
Integer b = 2;
15+
Integer c = 3;
16+
Integer d = 3;
17+
Integer e = 321;
18+
Integer f = 321;
19+
Long g = 3L;
20+
System.out.println(c == d);
21+
System.out.println(e == f);
22+
System.out.println(c == (a + b));
23+
System.out.println(c.equals(a + b));
24+
System.out.println(g == (a + b));
25+
System.out.println(g.equals(a + b));
26+
27+
}
28+
}
29+
30+
/*
31+
输出:
32+
true
33+
false
34+
true
35+
true
36+
true
37+
false
38+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.brianway.learning.java.base.constructor;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class FatherClass {
7+
private String name;
8+
9+
public FatherClass(){
10+
System.out.println("执行了父类的无参构造方法");
11+
}
12+
13+
public FatherClass(String name) {
14+
this.name = name;
15+
System.out.println("执行了父类的构造方法FatherClass(String name)");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.brianway.learning.java.base.constructor;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
7+
/**
8+
* 构造方法调用问题
9+
* 子类构造方法会首先默认调用父类的无参构造方法,无论是否显式写了super();
10+
*/
11+
public class SonClass extends FatherClass{
12+
public SonClass(String name) {
13+
//super(name);
14+
System.out.println("执行了子类的构造方法SonClass(String name)");
15+
}
16+
17+
public SonClass() {
18+
System.out.println("执行了子类的无参构造方法");
19+
}
20+
21+
public static void main(String[] args) {
22+
new SonClass("aaa");
23+
new SonClass();
24+
}
25+
26+
}
27+
28+
/*
29+
执行了父类的无参构造方法
30+
执行了子类的构造方法SonClass(String name)
31+
执行了父类的无参构造方法
32+
执行了子类的无参构造方法
33+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.brianway.learning.java.multithread.communication.example12;
2+
3+
import java.io.IOException;
4+
import java.io.PipedInputStream;
5+
6+
/**
7+
* Created by Brian on 2016/4/14.
8+
*/
9+
public class ReadData {
10+
public void readMethod(PipedInputStream input){
11+
try {
12+
System.out.println("read :");
13+
byte[]bytes = new byte[20];
14+
int readLength = input.read(bytes);
15+
while (readLength!=-1){
16+
String newData = new String(bytes,0,readLength);
17+
System.out.print(newData);
18+
readLength = input.read(bytes);
19+
}
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.brianway.learning.java.multithread.communication.example12;
2+
3+
import java.io.IOException;
4+
import java.io.PipedInputStream;
5+
import java.io.PipedOutputStream;
6+
7+
/**
8+
* Created by Brian on 2016/4/14.
9+
*/
10+
11+
/**
12+
* P173
13+
* 通过管道进行线程间通信:字节流
14+
*
15+
* 字符流PipedWriter,PipedReader,同理。
16+
*/
17+
public class Run12_pipeInputOutput {
18+
public static void main(String[] args) {
19+
try {
20+
WriteData writeData = new WriteData();
21+
ReadData readData = new ReadData();
22+
23+
PipedInputStream inputStream = new PipedInputStream();
24+
PipedOutputStream outputStream = new PipedOutputStream();
25+
26+
//inputStream.connect(outputStream);
27+
outputStream.connect(inputStream);
28+
29+
ThreadRead threadRead = new ThreadRead(readData,inputStream);
30+
threadRead.start();
31+
32+
Thread.sleep(2000);
33+
34+
ThreadWrite threadWrite = new ThreadWrite(writeData,outputStream);
35+
threadWrite.start();
36+
37+
} catch (IOException e) {
38+
e.printStackTrace();
39+
}catch (InterruptedException e){
40+
e.printStackTrace();
41+
}
42+
}
43+
}
44+
45+
46+
/*
47+
输出:
48+
read :
49+
write :
50+
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
51+
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
52+
53+
--------------
54+
取消注释//inputStream.connect(outputStream);
55+
输出:
56+
java.io.IOException: Already connected
57+
at java.io.PipedOutputStream.connect(PipedOutputStream.java:100)
58+
at com.brianway.learning.java.multithread.communication.example12.Run12_pipeInputOutput.main(Run12_pipeInputOutput.java:25)
59+
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
60+
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
61+
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
62+
at java.lang.reflect.Method.invoke(Method.java:483)
63+
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
64+
65+
66+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.brianway.learning.java.multithread.communication.example12;
2+
3+
import java.io.PipedInputStream;
4+
5+
/**
6+
* Created by Brian on 2016/4/14.
7+
*/
8+
public class ThreadRead extends Thread {
9+
private ReadData read;
10+
private PipedInputStream input;
11+
12+
public ThreadRead(ReadData read,PipedInputStream input) {
13+
super();
14+
this.input = input;
15+
this.read = read;
16+
}
17+
18+
@Override
19+
public void run() {
20+
read.readMethod(input);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.brianway.learning.java.multithread.communication.example12;
2+
3+
import java.io.PipedOutputStream;
4+
5+
/**
6+
* Created by Brian on 2016/4/14.
7+
*/
8+
public class ThreadWrite extends Thread{
9+
private WriteData write;
10+
private PipedOutputStream out;
11+
12+
public ThreadWrite(WriteData write, PipedOutputStream out) {
13+
super();
14+
this.write = write;
15+
this.out = out;
16+
}
17+
18+
@Override
19+
public void run() {
20+
write.writeMethod(out);
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.brianway.learning.java.multithread.communication.example12;
2+
3+
import java.io.IOException;
4+
import java.io.PipedOutputStream;
5+
6+
/**
7+
* Created by Brian on 2016/4/14.
8+
*/
9+
public class WriteData {
10+
public void writeMethod(PipedOutputStream out){
11+
try {
12+
System.out.println("write :");
13+
for(int i=0;i<300;i++){
14+
String outData = "" + (i+1);
15+
out.write(outData.getBytes());
16+
System.out.print(outData);
17+
}
18+
System.out.println();
19+
out.close();
20+
} catch (IOException e) {
21+
e.printStackTrace();
22+
}
23+
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.brianway.learning.java.multithread.communication.example13;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class BackupA extends Thread {
7+
private DBTools dbTools;
8+
9+
public BackupA(DBTools dbTools) {
10+
super();
11+
this.dbTools = dbTools;
12+
}
13+
14+
@Override
15+
public void run() {
16+
dbTools.backupA();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.brianway.learning.java.multithread.communication.example13;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class BackupB extends Thread {
7+
private DBTools dbTools;
8+
9+
public BackupB(DBTools dbTools) {
10+
super();
11+
this.dbTools = dbTools;
12+
}
13+
14+
@Override
15+
public void run() {
16+
dbTools.backupB();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.brianway.learning.java.multithread.communication.example13;
2+
3+
/**
4+
* Created by Brian on 2016/4/14.
5+
*/
6+
public class DBTools {
7+
volatile private boolean prevIsA = false;
8+
9+
synchronized public void backupA(){
10+
try {
11+
while (prevIsA == true){
12+
wait();
13+
}
14+
for(int i =0;i<5;i++){
15+
System.out.println("★★★★★");
16+
}
17+
prevIsA = true;
18+
notifyAll();
19+
} catch (InterruptedException e) {
20+
e.printStackTrace();
21+
}
22+
}
23+
24+
synchronized public void backupB(){
25+
try {
26+
while (prevIsA == false){
27+
wait();
28+
}
29+
for(int i =0;i<5;i++){
30+
System.out.println("☆☆☆☆☆");
31+
}
32+
prevIsA = false;
33+
notifyAll();
34+
} catch (InterruptedException e) {
35+
e.printStackTrace();
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)