java线程通讯
不同线程间进行通信通常有两种简单方法:
方法一 通过访问共享变量的方式(注:需要处理同步问题)
方法二 通过管道流
其中方法一有两种实现方法,即
方法一a)通过内部类实现线程的共享变量
代码如下:
Java代码
/**
* 通过内部类实现线程的共享变量
*
*/
Java代码
1.public class Innersharethread {
2. public static void main(String[] args) {
3. Mythread mythread = new Mythread();
4. mythread.getThread().start();
5. mythread.getThread().start();
6. mythread.getThread().start();
7. mythread.getThread().start();
8. }
9.}
10.class Mythread {
11. int index = 0;
12.
13. private class InnerThread extends Thread {
14. public synchronized void run() {
15. while (true) {
16. System.out.println(Thread.currentThread().getName()
17. + "is running and index is " + index++);
18. }
19. }
20. }
21.
22. public Thread getThread() {
23. return new InnerThread();
24. }
25.}
26.
27./**
28. * 通过内部类实现线程的共享变量
29. *
30. */
31.public class Innersharethread {
32. public static void main(String[] args) {
33. Mythread mythread = new Mythread();
34. mythread.getThread().start();
35. mythread.getThread().start();
36. mythread.getThread().start();
37. mythread.getThread().start();
38. }
39.}
40.class Mythread {
41. int index = 0;
42.
43. private class InnerThread extends Thread {
44. public synchronized void run() {
45. while (true) {
46. System.out.println(Thread.currentThread().getName()
47. + "is running and index is " + index++);
48. }
49. }
50. }
51.
52. public Thread getThread() {
53. return new InnerThread();
54. }
55.}
方法二b)通过实现Runnable接口实现线程的共享变量
代码如下
Java代码
Java代码
1./**
2.* 通过实现Runnable接口实现线程的共享变量
3.* @author Administrator
4.*
5.*/
6.public class Interfacaesharethread {
7.public static void main(String[] args) {
8.Mythread mythread = new Mythread();
9.new Thread(mythread).start();
10.new Thread(mythread).start();
11.new Thread(mythread).start();
12.new Thread(mythread).start();
13.}
14.}
15.
16./* 实现Runnable接口 */
17.class Mythread implements Runnable {
18.int index = 0;
19.
20.public synchronized void run() {
21.while (true)
22.System.out.println(Thread.currentThread().getName()
23.+ "is running and the index is " + index++);
24.}
25.}
26.
27./**
28.* 通过实现Runnable接口实现线程的共享变量
29.* @author Administrator
30.*
31.*/
32.public class Interfacaesharethread {
33.public static void main(String[] args) {
34.Mythread mythread = new Mythread();
35.new Thread(mythread).start();
36.new Thread(mythread).start();
37.new Thread(mythread).start();
38.new Thread(mythread).start();
39.}
40.}
41.
42./* 实现Runnable接口 */
43.class Mythread implements Runnable {
44.int index = 0;
45.
46.public synchronized void run() {
47.while (true)
48.System.out.println(Thread.currentThread().getName()
49.+ "is running and the index is " + index++);
50.}
51.}
52.方法二:
53.代码如下
54.Java代码
55.import java.io.IOException;
56.import java.io.PipedInputStream;
57.import java.io.PipedOutputStream;
58.
59.public class CommunicateWhitPiping {
60.public static void main(String[] args) {
61./**
62.* 创建管道输出流
63.*/
64.PipedOutputStream pos = new PipedOutputStream();
65./**
66.* 创建管道输入流
67.*/
68.PipedInputStream pis = new PipedInputStream();
69.try {
70./**
71.* 将管道输入流与输出流连接
72.* 此过程也可通过重载的构造函数来实现
73.*/
74.pos.connect(pis);
75.} catch (IOException e) {
76.e.printStackTrace();
77.}
78./**
79.* 创建生产者线程
80.*/
81.Producer p = new Producer(pos);
82./**
83.* 创建消费者线程
84.*/
85.Consumer c = new Consumer(pis);
86./**
87.* 启动线程
88.*/
89.p.start();
90.c.start();
91.}
92.}
93.
94./**
95.* 生产者线程(与一个管道输入流相关联)
96.*
97.*/
98.class Producer extends Thread {
99.private PipedOutputStream pos;
100.public Producer(PipedOutputStream pos) {
101.this.pos = pos;
102.}
103.public void run() {
104.int i = 8;
105.try {
106.pos.write(i);
107.} catch (IOException e) {
108.e.printStackTrace();
109.}
110.}
111.}
112.
113./**
114.* 消费者线程(与一个管道输入流相关联)
115.*
116.*/
117.class Consumer extends Thread {
118.private PipedInputStream pis;
119.public Consumer(PipedInputStream pis)
120.{
121.this.pis = pis;
122.}
123.public void run() {
124.try {
125.System.out.println(pis.read());
126.} catch (IOException e) {
127.e.printStackTrace();
128.}
129.}
130.}