最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)多線程輪流打印1-100的數(shù)字操作

 更新時(shí)間:2020年08月26日 11:28:59   作者:一只菜鳥(niǎo).....  
這篇文章主要介紹了Java實(shí)現(xiàn)多線程輪流打印1-100的數(shù)字操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

首先打印1-100數(shù)字如果用一個(gè)單線程實(shí)現(xiàn)那么只要一個(gè)for循環(huán)即可,那么如果要用兩個(gè)線程打印出來(lái)呢?(一個(gè)線程打印奇數(shù),一個(gè)線程打印偶數(shù))于是大家會(huì)想到可以通過(guò)加鎖實(shí)現(xiàn),但是這樣的效率是不是不高?這里我用一個(gè)變量來(lái)控制兩個(gè)線程的輸出

public class ThreadTest {
 volatile int flag=0;
 public void runThread() throws InterruptedException{
   Thread t1=new Thread(new Thread1());
   Thread t2=new Thread(new Thread2());
   t1.start();
   t2.start();
 }
 public class Thread1 implements Runnable{
 
 public void run() {
  int i=0;
  while(i<=99){
  if(flag==0)
  {
   System.out.println("t1="+i+"flag="+flag);
   i+=2;
   flag=1;
  }
  }
 } 
 }
 
 public class Thread2 implements Runnable{
 
 public void run() {
  int i=1;
  while(i<=99){
  if(flag==1)
  {
   System.out.println("t2="+i+"flag="+flag);
   i+=2;
   flag=0;
  }
  }
 }
 
 }
}

那么如果要實(shí)現(xiàn)三個(gè)線程輪流打印1-100的數(shù)字呢?是不是也可以用上面的方法實(shí)現(xiàn)呢?代碼如下

public class ThreadTest {
 private int i=0;
 private Thread thread1,thread2,thread3;
 private int flag=0;
 public void runThread() throws InterruptedException{
   thread1=new Thread(new Thread1());
   thread2=new Thread(new Thread2());
   thread3=new Thread(new Thread3());
   thread1.start();
   thread2.start();
   thread3.start();
 }
 public class Thread1 implements Runnable{
 
 public void run() {
  
  while(i<=100){
  if(flag==0) {
   System.out.println("t1="+i);
   i++;
   flag=1;
  }
  }
 } 
 }
 
 public class Thread2 implements Runnable{
 
 public void run() {
  
  while(i<=100){
  if(flag==1) {
   System.out.println("t2="+i);
   i++;
   flag=2;
  }
  }
 } 
 }
 
 public class Thread3 implements Runnable{
 
 public void run() {
  
  while(i<=100){
  if(flag==2) {
   System.out.println("t3="+i);
   i++;
   flag=0;
  }
  }
 }
 
 }
}

運(yùn)行結(jié)果

發(fā)現(xiàn)三個(gè)線程只打印了一次就停止不輸出了,是什么原因呢?

可以用jdk自帶的jstack來(lái)看看線程的狀態(tài),在windows系統(tǒng)中可以打開(kāi)cmd然后進(jìn)入jdk所在目錄,然后執(zhí)行Jsp,能查看到各線程id,然后執(zhí)行jstack -F pid就可以看的狀態(tài)了

可以看到幾個(gè)Thread state是BLOCKED,就是阻塞了,什么原因呢?

尷尬發(fā)現(xiàn)flag變量和i變量前面忘記加volatile,導(dǎo)致flag和i被線程讀取修改時(shí),其他線程不可見(jiàn),所以才導(dǎo)致上面的問(wèn)題出現(xiàn)。

在JVM中每個(gè)線程讀取變量到cache中時(shí)相互都是不可見(jiàn)的,也就是java五大內(nèi)存區(qū)中的程序計(jì)數(shù)器區(qū)域?qū)τ诿總€(gè)線程都是獨(dú)立的不共享的,只有堆內(nèi)存區(qū)和方法區(qū)是對(duì)所有線程都是共享的。

當(dāng)線程1讀取了flag和i的值,并對(duì)其進(jìn)行修改的時(shí)候,線程2并發(fā)運(yùn)行,并不知道flag和i值已經(jīng)改變,導(dǎo)致多線程數(shù)據(jù)不一致的情況,所以加了volatile后,當(dāng)線程讀取變量進(jìn)行修改后會(huì)“通知”其它線程這個(gè)值已經(jīng)進(jìn)行了修改。

import java.util.concurrent.atomic.AtomicInteger; 
public class ThreadTest {
 private volatile int i=0;
 private Thread thread1,thread2,thread3;
 private volatile int flag=0;
 public void runThread() throws InterruptedException{
   thread1=new Thread(new Thread1());
   thread2=new Thread(new Thread2());
   thread3=new Thread(new Thread3());
   thread1.start();
   thread2.start();
   thread3.start();
 }
 public class Thread1 implements Runnable{
 
 public void run() {
  while(i<100){
  if(flag==0) {
   System.out.println("t1="+i);
   i++;
   flag=1;
  }
  }
 }
 
 }
 
 public class Thread2 implements Runnable{
 
 public void run() {
  
  while(i<100){
  if(flag==1){
   System.out.println("t2="+i);
   i++;
   flag=2;
  }
  }
 }
 
 }
 
 public class Thread3 implements Runnable{
 
 public void run() {
  
  while(i<100){
  if(flag==2){
   System.out.println("t3="+i);
   i++;
   flag=0;
  }
  }
 }
 
 }
}

運(yùn)行結(jié)果

-----未完-----

補(bǔ)充知識(shí):Java n個(gè)線程輪流打印數(shù)字的問(wèn)題

一、兩個(gè)線程輪流打印數(shù)字。

加鎖實(shí)現(xiàn):

package lianxi;
 
/*
 * 用鎖實(shí)現(xiàn)兩個(gè)線程輪流打印1——100
 */
public class Print1TO100TwoThread {
 private Object lock = new Object();
 private int i = 0;
 
 Thread threadA = new Thread(new Runnable() {
 @Override
 public void run() {
  while (i <= 100) {
  synchronized (lock) {
 
   try {
   if (i > 100)
    break;
   System.out.println("threadA :" + (i++));
   lock.notify();
   lock.wait();
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
  }
  }
 }
 
 });
 
 Thread threadB = new Thread(new Runnable() {
 @Override
 public void run() {
  while (i <= 100) {
  synchronized (lock) {
 
   try {
   if (i > 100)
    break;
   System.out.println("threadB :" + (i++));
   lock.notify();
   lock.wait();
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
  }
  }
 }
 });
 
 public void startTwoThread() throws InterruptedException {
 threadA.start();
 Thread.sleep(20);
 threadB.start();
 }
 public static void main(String[] args) throws InterruptedException {
 new Print1TO100TwoThread().startTwoThread();
 } 
}

用鎖效率太低,用一個(gè)變量來(lái)控制打印的順序。

package lianxi;
/*
 * 用兩個(gè)線程輪流打印1——10;用所實(shí)現(xiàn)效率太低,用變量來(lái)控制
 */
public class PrinntNumTwoThread {
 
 private volatile int num = 0;
 private volatile boolean flag = false;
 
 Thread threadA = new Thread(new Runnable() {
 
 @Override
 public void run() {
  while (true) {
  if (num > 10)
   return;
  if (!flag) {
   System.out.println("threadA-->" + ":" + (num++));
   flag = !flag;
  }
  }
 }
 
 });
 
 Thread threadB = new Thread(new Runnable() {
 @Override
 public void run() {
  while (true) {
  if (num > 10)
   return;
  if (flag) {
   System.out.println("threadB-->" + ":" + (num++));
   flag = !flag;
  }
  }
 }
 
 });
 
 public void startTwoThread() {
 threadA.start();
 threadB.start();
 }
 
 public static void main(String[] args) {
 new PrinntNumTwoThread().startTwoThread();
 }
}

二、那么如果要實(shí)現(xiàn)三個(gè)線程輪流打印1-100的數(shù)字呢?

package lianxi; 
public class PrintNumThreeThread {
 private volatile int i = 0;
 private volatile int flag = 0;
 Thread threadA = new Thread(new Runnable() {
 @Override
 public void run() {
  while (true) {
  if (i > 100)
   return;
  if (flag == 0) {
   System.out.println("threadA->" + ":" + (i++));
   flag = 1;
  }
  }
 }
 
 });
 
 Thread threadB = new Thread(new Runnable() {
 @Override
 public void run() {
  while (true) {
  if (i > 100)
   return;
  if (flag == 1) {
   System.out.println("threadB->" + ":" + (i++));
   flag = 2;
  }
  }
 }
 
 });
 
 Thread threadC = new Thread(new Runnable() {
 @Override
 public void run() {
  while (true) {
  if (i > 100)
   return;
  if (flag == 2) {
   System.out.println("threadC->" + ":" + (i++));
   flag = 0;
  }
  }
 }
 });
 
 public void startThreeThread() {
 threadA.start();
 threadB.start();
 threadC.start();
 }
 
 public static void main(String[] args) {
 new PrintNumThreeThread().startThreeThread();
 }
}

以上這篇Java實(shí)現(xiàn)多線程輪流打印1-100的數(shù)字操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java實(shí)現(xiàn)List反轉(zhuǎn)的方法總結(jié)

    Java實(shí)現(xiàn)List反轉(zhuǎn)的方法總結(jié)

    在Java中,反轉(zhuǎn)一個(gè)List意味著將其元素的順序顛倒,使得第一個(gè)元素變成最后一個(gè),最后一個(gè)元素變成第一個(gè),依此類推,這一操作在處理數(shù)據(jù)集合時(shí)非常有用,所以本文給大家總結(jié)了Java實(shí)現(xiàn)List反轉(zhuǎn)的方法,需要的朋友可以參考下
    2024-04-04
  • 基于Arrays.sort()和lambda表達(dá)式

    基于Arrays.sort()和lambda表達(dá)式

    這篇文章主要介紹了Arrays.sort()和lambda表達(dá)式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 簡(jiǎn)易JDBC框架實(shí)現(xiàn)過(guò)程詳解

    簡(jiǎn)易JDBC框架實(shí)現(xiàn)過(guò)程詳解

    這篇文章主要介紹了簡(jiǎn)易JDBC框架實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • SpringBoot基于redis自定義注解實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)

    SpringBoot基于redis自定義注解實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn)

    本文主要介紹了SpringBoot基于redis自定義注解實(shí)現(xiàn)后端接口防重復(fù)提交校驗(yàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 你知道Java中的注解可以繼承嗎?

    你知道Java中的注解可以繼承嗎?

    注解想必大家都用過(guò),也叫元數(shù)據(jù),是一種代碼級(jí)別的注釋,可以對(duì)類或者方法等元素做標(biāo)記說(shuō)明。那么今天我想問(wèn)大家的是類被繼承了,注解能否繼承呢?可能會(huì)和大家想的不一樣,感興趣的可以往下看
    2022-12-12
  • Java中Cookie和Session的那些事兒

    Java中Cookie和Session的那些事兒

    Cookie和Session都是為了保持用戶的訪問(wèn)狀態(tài),一方面為了方便業(yè)務(wù)實(shí)現(xiàn),另一方面為了簡(jiǎn)化服務(wù)端的程序設(shè)計(jì)。這篇文章主要介紹了java中cookie和session的知識(shí),需要的朋友可以參考下
    2016-09-09
  • java byte與base64的互轉(zhuǎn)的實(shí)現(xiàn)示例

    java byte與base64的互轉(zhuǎn)的實(shí)現(xiàn)示例

    在項(xiàng)目開(kāi)發(fā)中經(jīng)常用到,比如前端上送文件流(byte[])到后臺(tái)并轉(zhuǎn)成文件,本文主要介紹了java byte與base64的互轉(zhuǎn)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)

    如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)

    這篇文章主要介紹了如何在Spring?Boot中使用OAuth2認(rèn)證和授權(quán)的相關(guān)資料,OAuth2.0是一種開(kāi)放的授權(quán)協(xié)議,它允許用戶授權(quán)第三方應(yīng)用訪問(wèn)其賬戶(或資源),而無(wú)需共享其用戶賬戶憑據(jù),需要的朋友可以參考下
    2023-12-12
  • Java 中的 File類詳情

    Java 中的 File類詳情

    這篇文章主要介紹了Java 中的 File類,對(duì)于File而言,其封裝的并不是一個(gè)真正存在的文件,僅僅是一個(gè)路徑名而已,下面我們來(lái)看看文章對(duì)File類的詳情介紹吧,需要的朋友也可以參考一下
    2021-11-11
  • 基于微信簽名signature獲取(實(shí)例講解)

    基于微信簽名signature獲取(實(shí)例講解)

    下面就為大家?guī)?lái)一篇基于微信簽名signature獲取(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09

最新評(píng)論

霍邱县| 杭锦后旗| 隆林| 平顶山市| 邢台县| 鄂托克前旗| 八宿县| 邹城市| 白银市| 邮箱| 年辖:市辖区| 海淀区| 北碚区| 浪卡子县| 云林县| 江北区| 汶川县| 南陵县| 福鼎市| 莎车县| 临安市| 西华县| 仁布县| 平顺县| 新田县| 南和县| 东乡| 镶黄旗| 会宁县| 长顺县| 富顺县| 莱州市| 丰台区| 安化县| 琼海市| 仲巴县| 岳池县| 麻城市| 北海市| 新兴县| 渑池县|