而在java中我们实现多线程间通信则主要采用"共享变量"和"管道流"这两种方法
其中方法一有两种实现方法,即
方法一a)通过内部类实现线程的共享变量
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
public
class
Innersharethread {
public
static
void
main(String[] args) {
Mythread mythread =
new
Mythread();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
}
}
class
Mythread {
int
index =
0
;
private
class
InnerThread
extends
Thread {
public
synchronized
void
run() {
while
(
true
) {
System.out.println(Thread.currentThread().getName()
+
"is running and index is "
+ index++);
}
}
}
public
Thread getThread() {
return
new
InnerThread();
}
}
/**
* 通过内部类实现线程的共享变量
*
*/
public
class
Innersharethread {
public
static
void
main(String[] args) {
Mythread mythread =
new
Mythread();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
mythread.getThread().start();
}
}
class
Mythread {
int
index =
0
;
private
class
InnerThread
extends
Thread {
public
synchronized
void
run() {
while
(
true
) {
System.out.println(Thread.currentThread().getName()
+
"is running and index is "
+ index++);
}
}
}
public
Thread getThread() {
return
new
InnerThread();
}
}
|
b)通过实现Runnable接口实现线程的共享变量
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public
class
Interfacaesharethread {
public
static
void
main(String[] args) {
Mythread mythread =
new
Mythread();
new
Thread(mythread).start();
new
Thread(mythread).start();
new
Thread(mythread).start();
new
Thread(mythread).start();
}
}
/* 实现Runnable接口 */
class
Mythread
implements
Runnable {
int
index =
0
;
public
synchronized
void
run() {
while
(
true
)
System.out.println(Thread.currentThread().getName() + "is running and
the index is " + index++);
}
}
/**
* 通过实现Runnable接口实现线程的共享变量
*/
public
class
Interfacaesharethread {
public
static
void
main(String[] args) {
Mythread mythread =
new
Mythread();
new
Thread(mythread).start();
new
Thread(mythread).start();
new
Thread(mythread).start();
new
Thread(mythread).start();
}
}
/* 实现Runnable接口 */
class
Mythread
implements
Runnable {
int
index =
0
;
public
synchronized
void
run() {
while
(
true
)
System.out.println(Thread.currentThread().getName() + "is running and
the index is " + index++);
}
}
|
方法二(通过管道流):
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
public
class
CommunicateWhitPiping {
public
static
void
main(String[] args) {
/**
* 创建管道输出流
*/
PipedOutputStream pos =
new
PipedOutputStream();
/**
* 创建管道输入流
*/
PipedInputStream pis =
new
PipedInputStream();
try
{
/**
* 将管道输入流与输出流连接 此过程也可通过重载的构造函数来实现
*/
pos.connect(pis);
}
catch
(IOException e) {
e.printStackTrace();
}
/**
* 创建生产者线程
*/
Producer p =
new
Producer(pos);
/**
* 创建消费者线程
*/
Consumer c =
new
Consumer(pis);
/**
* 启动线程
*/
p.start();
c.start();
}
}
/**
* 生产者线程(与一个管道输入流相关联)
*
*/
class
Producer
extends
Thread {
private
PipedOutputStream pos;
public
Producer(PipedOutputStream pos) {
this
.pos = pos;
}
public
void
run() {
int
i =
8
;
try
{
pos.write(i);
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
/**
* 消费者线程(与一个管道输入流相关联)
*
*/
class
Consumer
extends
Thread {
private
PipedInputStream pis;
public
Consumer(PipedInputStream pis) {
this
.pis = pis;
}
public
void
run() {
try
{
System.out.println(pis.read());
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
|