亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

Java實現指定線程執行順序的三種方式示例

發布時間:2020-09-30 15:38:24 來源:腳本之家 閱讀:125 作者:Leon-Zheng 欄目:編程語言

本文實例講述了Java實現指定線程執行順序的三種方式。分享給大家供大家參考,具體如下:

方法一:通過共享對象鎖加上可見變量來實現。

public class MyService {
  private volatile int orderNum = 1;
  public synchronized void methodA() {
    try {
      while (orderNum != 1) {
        wait();
      }
      for (int i = 0; i < 2; i++) {
        System.out.println("AAAAA");
      }
      orderNum = 2;
      notifyAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  public synchronized void methodB() {
    try {
      while (orderNum != 2) {
        wait();
      }
      for (int i = 0; i < 2; i++) {
        System.out.println("BBBBB");
      }
      orderNum = 3;
      notifyAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  public synchronized void methodC() {
    try {
      while (orderNum != 3) {
        wait();
      }
      for (int i = 0; i < 2; i++) {
        System.out.println("CCCCC");
      }
      orderNum = 1;
      notifyAll();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

import service.MyService;
public class ThreadAA extends Thread {
  private MyService dbtools;
  public ThreadAA(MyService dbtools) {
    super();
    this.dbtools = dbtools;
  }
  @Override
  public void run() {
    dbtools.methodA();
  }
}

import service.MyService;
public class ThreadBB extends Thread {
  private MyService dbtools;
  public ThreadBB(MyService dbtools) {
    super();
    this.dbtools = dbtools;
  }
  @Override
  public void run() {
    dbtools.methodB();
  }
}

import service.MyService;
public class ThreadCC extends Thread {
  private MyService dbtools;
  public ThreadCC(MyService dbtools) {
    this.dbtools = dbtools;
  }
  @Override
  public void run() {
    dbtools.methodC();
  }
}

import extthread.ThreadCC;
import service.MyService;
import extthread.ThreadAA;
import extthread.ThreadBB;
public class Run {
  public static void main(String[] args) {
    MyService myService = new MyService();
    for (int i = 0; i < 2; i++) {
      ThreadBB output = new ThreadBB(myService);
      output.start();
      ThreadAA input = new ThreadAA(myService);
      input.start();
      ThreadCC threadCC = new ThreadCC(myService);
      threadCC.start();
    }
  }
}

執行結果:

Java實現指定線程執行順序的三種方式示例

可以看到線程的啟動按順序執行了。共享對象鎖,可以保證每個方法只能同時有一個線程進入,配合wait和notifyall方法,可以啟動或者喚醒線程。

方法二:通過主線程Join()

class T11 extends Thread {
  public void run() {
    System.out.println("in T1");
  }
}
class T22 extends Thread {
  public void run() {
    System.out.println("in T2");
  }
}
class T33 extends Thread {
  public void run() {
    System.out.println("in T3");
  }
}
public class Test2 {
  public static void main(String[] args) throws InterruptedException {
    T11 t1 = new T11();
    T22 t2 = new T22();
    T33 t3 = new T33();
    t1.start();
    t1.join();
    t2.start();
    t2.join();
    t3.start();
  }
}

方法三:通過線程執行時Join()

class T1 extends Thread {
  public void run(){
    Random random = new Random();
    try {
      Thread.sleep(random.nextInt(1000));
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("in T1");
  }
}
class T2 extends Thread{
  private Thread thread;
  public T2(Thread thread) {
    this.thread = thread;
  }
  public void run(){
    try {
      thread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("in T2");
  }
}
class T3 extends Thread{
  private Thread thread;
  public T3(Thread thread) {
    this.thread = thread;
  }
  public void run(){
    try {
      thread.join();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    System.out.println("in T3");
  }
}
public class Test {
  public static void main(String[] args) throws InterruptedException {
    T1 t1 = new T1();
    T2 t2 = new T2(t1);
    T3 t3 = new T3(t2);
    t2.start();
    t1.start();
    t3.start();
  }
}

更多java相關內容感興趣的讀者可查看本站專題:《Java進程與線程操作技巧總結》、《Java數據結構與算法教程》、《Java操作DOM節點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

向AI問一下細節

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

AI

新昌县| 玛沁县| 山西省| 壤塘县| 鄂托克前旗| 六安市| 岢岚县| 扎鲁特旗| 定西市| 万年县| 保定市| 石狮市| 焉耆| 吴忠市| 长丰县| 合水县| 介休市| 大连市| 石狮市| 休宁县| 邵阳县| 张家界市| 惠来县| 郑州市| 湖口县| 新乡县| 霞浦县| 鄂伦春自治旗| 天门市| 剑川县| 陆良县| 东台市| 兴和县| 东山县| 屏边| 邓州市| 耿马| 全南县| 朝阳区| 宜春市| 瑞丽市|