java实现多线程交替打印两个数

2022-07-27,,,,

本文实例为大家分享了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实现多线程交替打印两个数.doc》

下载本文的Word格式文档,以方便收藏与打印。