91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在java項目中利用多線程怎么交替打印兩個數

發布時間:2020-11-24 14:49:27 來源:億速云 閱讀:164 作者:Leah 欄目:開發技術

在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項目中利用多線程怎么交替打印兩個數的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

义马市| 隆尧县| 江津市| 莎车县| 马公市| 柘城县| 勃利县| 班戈县| 宝清县| 茌平县| 同德县| 开鲁县| 正蓝旗| 蓬安县| 宾阳县| 陕西省| 黄陵县| 麻江县| 刚察县| 新竹县| 温宿县| 汉中市| 宁德市| 桑植县| 罗源县| 通州区| 色达县| 象山县| 鹿泉市| 申扎县| 衢州市| 阿拉善右旗| 永州市| 宽甸| 嵊泗县| 兰坪| 黑龙江省| 台江县| 葫芦岛市| 安西县| 鄂尔多斯市|