系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > 系统教程 > 其它教程 > 详细页面

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

时间:2020-11-23来源:www.pcxitongcheng.com作者:电脑系统城

本文实例为大家分享了java实现多线程交替打印两个数的具体代码,供大家参考,具体内容如下

方法1、使用wait和notify

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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自旋锁

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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)

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

分享到:

相关信息

  • ThinkPad蓝牙鼠标如何配对

    ThinkPad蓝牙鼠标如何配对解答步骤41U5008鼠标驱动官网地址: https://support.lenovo.com/en_US/downloads/detail.page?&LegacyDocID=MIGR-67201 第一种方式是比较传统的:使...

    2024-04-11

  • USB接口无法识别设备的解决方法

    故障现象: USB设备U盘、移动硬盘等插入后提示无法识别的设备,确认设备本身正常,设备可加电,或插入设备后加电但无任何反应,无法使用。新型号机器多表现为黄色USB接口存在此问题,...

    2024-04-11

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载