您的位置:首页 > 编程语言 > Java开发

java线程通信

2016-05-14 16:01 405 查看
1.传统的线程通信,直接上代码:

<pre name="code" class="java">package com.norelax.www;
//传统的线程通信,主线程执行100次,子线程执行50次,主线程和子线程交替执行
public class TraditionalThreadCommunication {
public static void main(String[] args) {
final Business business=new Business();
new Thread(new Runnable() {
public void run() {
business.sub();
}
}).start();
business.main();
}

}

class Business{
//标志位用来标示是否要执行子线程
boolean bShouldSub=true;
//子线程执行方法
public synchronized void sub() {
while (!bShouldSub) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 1; i <= 50; i++) {
System.out.println("sub thread sequence of " +i);
}
bShouldSub=false;
//唤醒主线程
this.notify();
}
//主线程执行方法
public synchronized void main(){
while (bShouldSub) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i = 1; i <= 100; i++) {
System.out.println("main thread sequence of " +i);
}
bShouldSub=true;
//唤醒子线程
this.notify();

}
}



2.java JDK5之后的线程通信,直接上代码:

<pre name="code" class="java">package com.norelax.www;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* 三种线程之间的通信问题
*
* @author wusong 实例:主线程循环50次后休眠,叫醒子线程sub2执行10后休眠,叫醒子线程sub3执行20次后休眠,叫醒主线程
*/
public class ThreeThreadCommunication {

public static void main(String[] args) {
final Business business = new Business();
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10; i++) {
business.sub2();
}
}
}).start();

new Thread(){
public void run() {
for (int i = 0; i < 10; i++) {
business.sub3();
}
}}.start();
for (int i = 0; i < 10; i++) {
business.main();
}
}
static class Business {
Lock lock=new ReentrantLock();
Condition condition1 = lock.newCondition();
Condition condition2 = lock.newCondition();
Condition condition3 = lock.newCondition();
//线程执行的标志位
int bShouldSub=1;
//主线程方法
public void main(){
//加锁
lock.lock();
try {
while (bShouldSub!=1) {
condition1.await();
}
for (int i = 1; i <=50; i++) {
System.out.println("main thread sequence of "+i);
}
bShouldSub=2;
//唤醒sub2线程
condition2.signal();
} catch (Exception e) {
e.printStackTrace();
}finally{
//最终锁要示释放
lock.unlock();
}
}
//子线程2的方法
public void sub2(){
//加锁
lock.lock();
try {
while (bShouldSub!=2) {
condition2.await();
}
for (int i = 1; i <=10; i++) {
System.out.println("sub2 thread sequence of "+i);
}
bShouldSub=3;
//唤醒sub3线程
condition3.signal();
} catch (Exception e) {
e.printStackTrace();
}finally{
//最终锁要示释放
lock.unlock();
}
}
//子线程3的方法
public void sub3(){
//加锁
lock.lock();
try {
while (bShouldSub!=3) {
condition3.await();
}
for (int i = 1; i <=20; i++) {
System.out.println("sub3 thread sequence of "+i);
}
bShouldSub=1;
//唤醒sub3线程
condition1.signal();
} catch (Exception e) {
e.printStackTrace();
}finally{
//最终锁要示释放
lock.unlock();
}

}
}
}


运行结果如下:

main thread sequence of 1
main thread sequence of 2
main thread sequence of 3
main thread sequence of 4
main thread sequence of 5
main thread sequence of 6
main thread sequence of 7
main thread sequence of 8
main thread sequence of 9
main thread sequence of 10
main thread sequence of 11
main thread sequence of 12
main thread sequence of 13
main thread sequence of 14
main thread sequence of 15
main thread sequence of 16
main thread sequence of 17
main thread sequence of 18
main thread sequence of 19
main thread sequence of 20
main thread sequence of 21
main thread sequence of 22
main thread sequence of 23
main thread sequence of 24
main thread sequence of 25
main thread sequence of 26
main thread sequence of 27
main thread sequence of 28
main thread sequence of 29
main thread sequence of 30
main thread sequence of 31
main thread sequence of 32
main thread sequence of 33
main thread sequence of 34
main thread sequence of 35
main thread sequence of 36
main thread sequence of 37
main thread sequence of 38
main thread sequence of 39
main thread sequence of 40
main thread sequence of 41
main thread sequence of 42
main thread sequence of 43
main thread sequence of 44
main thread sequence of 45
main thread sequence of 46
main thread sequence of 47
main thread sequence of 48
main thread sequence of 49
main thread sequence of 50
sub2 thread sequence of 1
sub2 thread sequence of 2
sub2 thread sequence of 3
sub2 thread sequence of 4
sub2 thread sequence of 5
sub2 thread sequence of 6
sub2 thread sequence of 7
sub2 thread sequence of 8
sub2 thread sequence of 9
sub2 thread sequence of 10
sub3 thread sequence of 1
sub3 thread sequence of 2
sub3 thread sequence of 3
sub3 thread sequence of 4
sub3 thread sequence of 5
sub3 thread sequence of 6
sub3 thread sequence of 7
sub3 thread sequence of 8
sub3 thread sequence of 9
sub3 thread sequence of 10
sub3 thread sequence of 11
sub3 thread sequence of 12
sub3 thread sequence of 13
sub3 thread sequence of 14
sub3 thread sequence of 15
sub3 thread sequence of 16
sub3 thread sequence of 17
sub3 thread sequence of 18
sub3 thread sequence of 19
sub3 thread sequence of 20
main thread sequence of 1
main thread sequence of 2
main thread sequence of 3
main thread sequence of 4
main thread sequence of 5
main thread sequence of 6
main thread sequence of 7
main thread sequence of 8
main thread sequence of 9
main thread sequence of 10
main thread sequence of 11
main thread sequence of 12
main thread sequence of 13
main thread sequence of 14
main thread sequence of 15
main thread sequence of 16
main thread sequence of 17
main thread sequence of 18
main thread sequence of 19
main thread sequence of 20
main thread sequence of 21
main thread sequence of 22
main thread sequence of 23
main thread sequence of 24
main thread sequence of 25
main thread sequence of 26
main thread sequence of 27
main thread sequence of 28
main thread sequence of 29
main thread sequence of 30
main thread sequence of 31
main thread sequence of 32
main thread sequence of 33
main thread sequence of 34
main thread sequence of 35
main thread sequence of 36
main thread sequence of 37
main thread sequence of 38
main thread sequence of 39
main thread sequence of 40
main thread sequence of 41
main thread sequence of 42
main thread sequence of 43
main thread sequence of 44
main thread sequence of 45
main thread sequence of 46
main thread sequence of 47
main thread sequence of 48
main thread sequence of 49
main thread sequence of 50
sub2 thread sequence of 1
sub2 thread sequence of 2
sub2 thread sequence of 3
sub2 thread sequence of 4
sub2 thread sequence of 5
sub2 thread sequence of 6
sub2 thread sequence of 7
sub2 thread sequence of 8
sub2 thread sequence of 9
sub2 thread sequence of 10
sub3 thread sequence of 1
sub3 thread sequence of 2
sub3 thread sequence of 3
sub3 thread sequence of 4
sub3 thread sequence of 5
sub3 thread sequence of 6
sub3 thread sequence of 7
sub3 thread sequence of 8
sub3 thread sequence of 9
sub3 thread sequence of 10
sub3 thread sequence of 11
sub3 thread sequence of 12
sub3 thread sequence of 13
sub3 thread sequence of 14
sub3 thread sequence of 15
sub3 thread sequence of 16
sub3 thread sequence of 17
sub3 thread sequence of 18
sub3 thread sequence of 19
sub3 thread sequence of 20
main thread sequence of 1
main thread sequence of 2
main thread sequence of 3
main thread sequence of 4
main thread sequence of 5
main thread sequence of 6
main thread sequence of 7
main thread sequence of 8
main thread sequence of 9
main thread sequence of 10
main thread sequence of 11
main thread sequence of 12
main thread sequence of 13
main thread sequence of 14
main thread sequence of 15
main thread sequence of 16
main thread sequence of 17
main thread sequence of 18
main thread sequence of 19
main thread sequence of 20
main thread sequence of 21
main thread sequence of 22
main thread sequence of 23
main thread sequence of 24
main thread sequence of 25
main thread sequence of 26
main thread sequence of 27
main thread sequence of 28
main thread sequence of 29
main thread sequence of 30
main thread sequence of 31
main thread sequence of 32
main thread sequence of 33
main thread sequence of 34
main thread sequence of 35
main thread sequence of 36
main thread sequence of 37
main thread sequence of 38
main thread sequence of 39
main thread sequence of 40
main thread sequence of 41
main thread sequence of 42
main thread sequence of 43
main thread sequence of 44
main thread sequence of 45
main thread sequence of 46
main thread sequence of 47
main thread sequence of 48
main thread sequence of 49
main thread sequence of 50
sub2 thread sequence of 1
sub2 thread sequence of 2
sub2 thread sequence of 3
sub2 thread sequence of 4
sub2 thread sequence of 5
sub2 thread sequence of 6
sub2 thread sequence of 7
sub2 thread sequence of 8
sub2 thread sequence of 9
sub2 thread sequence of 10
sub3 thread sequence of 1
sub3 thread sequence of 2
sub3 thread sequence of 3
sub3 thread sequence of 4
sub3 thread sequence of 5
sub3 thread sequence of 6
sub3 thread sequence of 7
sub3 thread sequence of 8
sub3 thread sequence of 9
sub3 thread sequence of 10
sub3 thread sequence of 11
sub3 thread sequence of 12
sub3 thread sequence of 13
sub3 thread sequence of 14
sub3 thread sequence of 15
sub3 thread sequence of 16
sub3 thread sequence of 17
sub3 thread sequence of 18
sub3 thread sequence of 19
sub3 thread sequence of 20
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: