java线程的通信_java线程之间的通信

1.常用的方法

sleep() 该线程进入等待状态,不释放锁

wait()该线程进入等待状态,释放锁

notify()随机唤醒一个线程

notifyAll()唤醒全部线程

getName()获取线程对象的名称。默认情况下,名字的组成 Thread-编号(编号从0开始)

setName(String name)设置线程名称

currentThread()返回当前正在执行的线程对象引用

sleep(Long time)让当前线程休眠time毫秒

setDaemon(boolean on)设置线程为守护线程,一旦前台(主线程),结束,守护线程就结束了

join()当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续

join(int time)  当前线程暂停, 等待指定的线程执行time秒结束后, 当前线程再继续

yield()暂停当前正在执行的线程对象,并执行其他线程。

getPriority()获取线程优先级

setPriority(int newPriority)更改线程的优先级

2.线程之间的通信

a.两个线程之间的通信

public class ThreadExchange {

@Test

public void test2Thread() {

MyPrint myPrint = new MyPrint();

new Thread(new Runnable() {

@Override

public void run() {

while (true) {

try {

myPrint.printA();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

new Thread(new Runnable() {

@Override

public void run() {

while (true) {

try {

myPrint.printB();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

}

}

class MyPrint {

private Integer flag = 0;

public synchronized void printA() throws InterruptedException {

if (flag != 0) {

this.wait();

}

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.println("a");

flag = 1;

this.notify();

}

public synchronized void printB() throws InterruptedException {

if (flag != 1) {

this.wait();

}

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.println("b");

flag = 0;

this.notify();

}

}

b.三个以上的线程之间的通信

方式一

public classThreadExchange {

@Testpublic voidtest2Thread() {

MyPrint myPrint= newMyPrint();new Thread(newRunnable() {

@Overridepublic voidrun() {while (true) {try{

myPrint.printA();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();new Thread(newRunnable() {

@Overridepublic voidrun() {while (true) {try{

myPrint.printB();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}).start();

}

}classMyPrint {private Integer flag = 0;public synchronized void printA() throwsInterruptedException {if (flag != 0) {this.wait();

}

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.println("a");

flag= 1;this.notify();

}public synchronized void printB() throwsInterruptedException {if (flag != 1) {this.wait();

}

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.println("b");

flag= 0;this.notify();

}

}2.三个以上的线程之间的通信public classThreadExchange {

@Testpublic voidtest2Thread() {final MyPrint p = newMyPrint();newThread() {public voidrun() {while (true) {try{

p.print1();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();newThread() {public voidrun() {while (true) {try{

p.print2();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();newThread() {public voidrun() {while (true) {try{

p.print3();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();

}

}classMyPrint {private int flag = 1;public void print1() throwsInterruptedException {synchronized(this) {while(flag != 1) {this.wait();

}

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.println("a");

flag= 2;this.notifyAll();

}

}public void print2() throwsInterruptedException {synchronized (this) {while (flag != 2) {this.wait();

}

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.println("b");

flag= 3;this.notifyAll();

}

}public void print3() throwsInterruptedException {synchronized (this) {while (flag != 3) {this.wait();

}

System.out.print("c");

System.out.print("c");

System.out.print("c");

System.out.print("c");

System.out.println("c");

flag= 1;this.notifyAll();

}

}

}

方式二

public classThreadExchange {

@Testpublic voidtest3Thread() {final MyPrint p = newMyPrint();newThread() {public voidrun() {while (true) {try{

p.print1();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();newThread() {public voidrun() {while (true) {try{

p.print2();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();newThread() {public voidrun() {while (true) {try{

p.print3();

}catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}.start();

}

}classMyPrint {private ReentrantLock r = newReentrantLock();private Condition c1 =r.newCondition();private Condition c2 =r.newCondition();private Condition c3 =r.newCondition();private int flag = 1;public void print1() throwsInterruptedException {

r.lock();//获取锁

if (flag != 1) {

c1.await();

}

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.print("a");

System.out.println("a");

flag= 2;

c2.signal();

r.unlock();//释放锁

}public void print2() throwsInterruptedException {

r.lock();if (flag != 2) {

c2.await();

}

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.print("b");

System.out.println("b");

flag= 3;

c3.signal();

r.unlock();

}public void print3() throwsInterruptedException {

r.lock();if (flag != 3) {

c3.await();

}

System.out.print("c");

System.out.print("c");

System.out.print("c");

System.out.print("c");

System.out.println("c");

flag= 1;

c1.signal();

r.unlock();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值