欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-22 14:11:46
本文实例为大家分享了java实现多线程交替打印两个数的具体代码,供大家参考,具体内容如下方法1、使用wait和notifypackage com.thread; public class t01 {...

本文实例为大家分享了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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。