亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何確定線程的執行順序

如何確定線程的執行順序

1996青年 2016-11-05 21:31:45
package com.oracle.thread;import java.util.concurrent.CountDownLatch;public class PrimeNumber implements Runnable{ int num; static int sum = 0; public PrimeNumber(int num) { this.num=num; } final Integer mutex = 1; CountDownLatch latch = new CountDownLatch(3) ; @Override public ?void run() { try { synchronized (mutex) { int i = num-1000+1; while(i<num){ if(isPrime(i)){ System.out.println(Thread.currentThread().getName()+":"+i); sum++; } i++; } } } catch (Exception e) { }finally { latch.countDown(); System.out.println(sum); } } public static boolean isPrime(int n){ for(int i = 2;i<n ;i++){ if(n%i==0) return false; } return ?true; } public static void main(String[] args) { PrimeNumber p1 = new PrimeNumber(1000); PrimeNumber p2 = new PrimeNumber(2000); PrimeNumber p3 = new PrimeNumber(3000); Thread t1 = new Thread(p1,"線程1"); t1.setPriority(Thread.MAX_PRIORITY); Thread t2 = new Thread(p2,"線程2"); t2.setPriority(Thread.NORM_PRIORITY); Thread t3 = new Thread(p3,"線程3"); t3.setPriority(Thread.MIN_PRIORITY); t1.start(); t2.start(); t3.start(); }}我想讓線程穩定的按照t1 t2 t3 的順序執行 優先級并不能穩定的確定執行順序 ?有沒有更好的辦法
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 1207 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號