在java项目中利用多线程怎么交替打印两个数

发布时间:2020-11-24 14:49:27 作者:Leah
来源:亿速云 阅读:164

在java项目中利用多线程怎么交替打印两个数?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

方法1、使用wait和notify

package com.thread;
 
public class T01 {
 
 public static void main(String[] args) {
 char[] char1 = "AAAAAA".toCharArray();
 char[] char2 = "BBBBBB".toCharArray();
 Object object = new Object();
 
 Thread thread1 = new Thread(() -> {
  synchronized(object){//使用notify和wait时,必须要选获取到锁
   for (int i = 0; i < char1.length; i++) {
   try {
    System.out.print(char1[i]);
    object.notify();
    object.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   }
   object.notify();//必须加上,否则程序无法结束,两个线程总有一个最后是wait状态,所以此处必须加
  }
  
 },"t1");
 
 
 Thread thread2 = new Thread( () -> {
  synchronized(object){
   for (int i = 0; i < char2.length; i++) {
   try {
    System.out.print(char2[i]);
    object.notify();
    object.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   }
   object.notify();
  }
 },"t2");
 
 thread1.start();
 thread2.start();
 
 }
}

方法2、使用LockSupport方法

package com.thread;
 
import java.util.concurrent.locks.LockSupport;
 
public class T02 {
 static Thread thread1 ;
 static Thread thread2 ;
 public static void main(String[] args) {
 char[] char1 = "AAAAAA".toCharArray();
 char[] char2 = "BBBBBB".toCharArray();
 
 thread1 = new Thread(() -> {
  for (int i = 0; i < char1.length; i++) {
   System.out.print(char1[i]);
   LockSupport.unpark(thread2);
   LockSupport.park();
  }
 },"t1");
 
  thread2 = new Thread(() -> {
  for (int i = 0; i < char2.length; i++) {
   LockSupport.park();
   System.out.print(char2[i]);
   LockSupport.unpark(thread1);
  }
 },"t2");
 
 thread1.start();
 thread2.start();
 
 }
 
 
}

方法3、使用CAS自旋锁

package com.thread;
 
public class T03 {
 enum ReadEnum{
 T1,
 T2;
 }
 static volatile ReadEnum r = ReadEnum.T1;
 public static void main(String[] args) {
 char[] char1 = "AAAAAA".toCharArray();
 char[] char2 = "BBBBBB".toCharArray();
 Thread thread1 = new Thread(() ->{
  for (int i = 0; i < char1.length; i++) {
  while (r != ReadEnum.T1) {
  }
  System.out.print(char1[i]);
  r = ReadEnum.T2;
  }
 },"t1");
 
 
 Thread thread2 = new Thread(() ->{
  for (int i = 0; i < char2.length; i++) {
  while (r != ReadEnum.T2) {
  }
  System.out.print(char2[i]);
  r = ReadEnum.T1;
  }
 },"t2");
 
 thread1.start();
 thread2.start();
 
 
 }
}

方法4、使用Condition方法

package com.thread;
 
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
 
 
public class T04 {
 
 public static void main(String[] args) {
 char[] char1 = "AAAAAA".toCharArray();
 char[] char2 = "BBBBBB".toCharArray();
 
 ReentrantLock lock = new ReentrantLock();
 Condition condition1 = lock.newCondition();
 Condition condition2 = lock.newCondition();
 
 Thread thread1 = new Thread(() ->{
  try {
  lock.lock();
  for (int i = 0; i < char1.length; i++) {
   System.out.print(char1[i]);
   condition2.signal();//唤醒线程2执行
   condition1.await();//线程1等待
  }
  condition2.signal();
  }catch (Exception e) {
  e.printStackTrace();
  }finally{
  lock.unlock();
  }
 },"t1");
 
 Thread thread2 = new Thread(() ->{
  try {
  lock.lock();
  for (int i = 0; i < char2.length; i++) {
   System.out.print(char2[i]);
   condition1.signal();
   condition2.await();
  }
  condition1.signal();
  } catch (Exception e) {
  e.printStackTrace();
  }finally{
  lock.unlock();
  }
 
 },"t2");
 
 thread1.start();
 thread2.start();
 
 
 }
}

Condition与notify相比的好处是,Condition可以指定需要唤醒的线程,而notify是无法指定的,只能随机唤醒一个或者全唤醒(notifyAll)

看完上述内容,你们掌握在java项目中利用多线程怎么交替打印两个数的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. java 多线程–线程交替
  2. java怎么实现两个线程交替打印

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 多线程 ava

上一篇:在JavaScript中字符串中的[]怎么使用正则表达式去除

下一篇:.py配置文件怎么在python中使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》