程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 基於Java回想之多線程同步的應用詳解

基於Java回想之多線程同步的應用詳解

編輯:關於JAVA

基於Java回想之多線程同步的應用詳解。本站提示廣大學習愛好者:(基於Java回想之多線程同步的應用詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是基於Java回想之多線程同步的應用詳解正文


起首論述甚麼是同步,分歧步有甚麼成績,然後評論辯論可以采用哪些辦法掌握同步,接上去我們會模仿回想收集通訊時那樣,構建一個辦事器真個“線程池”,JDK為我們供給了一個很年夜的concurrent對象包,最初我們會對外面的內容停止摸索。

為何要線程同步?

說到線程同步,年夜部門情形下, 我們是在針對“單對象多線程”的情形停止評論辯論,普通會將其分紅兩部門,一部門是關於“同享變量”,一部門關於“履行步調”。

同享變量

當我們在線程對象(Runnable)中界說了全局變量,run辦法會修正該變量時,假如有多個線程同時應用該線程對象,那末就會形成全局變量的值被同時修正,形成毛病。我們來看上面的代碼:

同享變量形成同步成績
 class MyRunner implements Runnable
 {
     public int sum = 0;

     public void run()
     {
         System.out.println(Thread.currentThread().getName() + " Start.");
         for (int i = 1; i <= 100; i++)
         {
             sum += i;
         }
         try {
             Thread.sleep(500);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         System.out.println(Thread.currentThread().getName() + " --- The value of sum is " + sum);
         System.out.println(Thread.currentThread().getName() + " End.");
     }
 }

 
 private static void sharedVaribleTest() throws InterruptedException
 {
     MyRunner runner = new MyRunner();
     Thread thread1 = new Thread(runner);
     Thread thread2 = new Thread(runner);
     thread1.setDaemon(true);
     thread2.setDaemon(true);
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
 }

這個示例中,線程用來盤算1到100的和是若干,我們曉得准確成果是5050(似乎是高斯小時刻玩過這個?),然則上述法式前往的成果是10100,緣由是兩個線程同時對sum停止操作。

履行步調

我們在多個線程運轉時,能夠須要某些操作合在一路作為“原子操作”,即在這些操作可以看作是“單線程”的,例如我們能夠願望輸入成果的模樣是如許的:

線程1:步調1
 線程1:步調2
 線程1:步調3
 線程2:步調1
 線程2:步調2
 線程2:步調3

假如同步掌握欠好,出來的模樣能夠是如許的:

線程1:步調1
線程2:步調1
線程1:步調2
線程2:步調2
線程1:步調3
線程2:步調3

這裡我們也給出一個示例代碼:

履行步調凌亂帶來的同步成績
 class MyNonSyncRunner implements Runnable
 {
     public void run() {
         System.out.println(Thread.currentThread().getName() + " Start.");
         for(int i = 1; i <= 5; i++)
         {
             System.out.println(Thread.currentThread().getName() + " Running step " + i);
             try
             {
                 Thread.sleep(50);
             }
             catch(InterruptedException ex)
             {
                 ex.printStackTrace();
             }
         }
         System.out.println(Thread.currentThread().getName() + " End.");
     }
 }

 
 private static void syncTest() throws InterruptedException
 {
     MyNonSyncRunner runner = new MyNonSyncRunner();
     Thread thread1 = new Thread(runner);
     Thread thread2 = new Thread(runner);
     thread1.setDaemon(true);
     thread2.setDaemon(true);
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
 }

若何掌握線程同步

既然線程同步有上述成績,那末我們應當若何去處理呢?針對分歧緣由形成的同步成績,我們可以采用分歧的戰略。

掌握同享變量

我們可以采用3種方法來掌握同享變量。

將“單對象多線程”修正成“多對象多線程”

上文說起,同步成績普通產生在“單對象多線程”的場景中,那末最簡略的處置方法就是將運轉模子修正成“多對象多線程”的模樣,針對下面示例中的同步成績,修正後的代碼以下:

處理同享變量成績計劃一
 private static void sharedVaribleTest2() throws InterruptedException
 {
     Thread thread1 = new Thread(new MyRunner());
     Thread thread2 = new Thread(new MyRunner());
     thread1.setDaemon(true);
     thread2.setDaemon(true);
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
 }

我們可以看到,上述代碼中兩個線程應用了兩個分歧的Runnable實例,它們在運轉進程中,就不會去拜訪統一個全局變量。
將“全局變量”升級為“部分變量”

既然是同享變量形成的成績,那末我們可以將同享變量改成“不同享”,行將其修正為部分變量。如許也能夠處理成績,異樣針對下面的示例,這類處理方法的代碼以下:

處理同享變量成績計劃二
 class MyRunner2 implements Runnable
 {
     public void run()
     {
         System.out.println(Thread.currentThread().getName() + " Start.");
         int sum = 0;
         for (int i = 1; i <= 100; i++)
         {
             sum += i;
         }
         try {
             Thread.sleep(500);
         } catch (InterruptedException e) {
             e.printStackTrace();
         }
         System.out.println(Thread.currentThread().getName() + " --- The value of sum is " + sum);
         System.out.println(Thread.currentThread().getName() + " End.");
     }
 }

 
 private static void sharedVaribleTest3() throws InterruptedException
 {
     MyRunner2 runner = new MyRunner2();
     Thread thread1 = new Thread(runner);
     Thread thread2 = new Thread(runner);
     thread1.setDaemon(true);
     thread2.setDaemon(true);
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
 }

我們可以看出,sum變量曾經由全局變質變為run辦法外部的部分變量了。
應用ThreadLocal機制

ThreadLocal是JDK引入的一種機制,它用於處理線程間同享變量,應用ThreadLocal聲明的變量,即便在線程中屬於全局變量,針對每一個線程來說,這個變量也是自力的。

我們可以用這類方法來改革下面的代碼,以下所示:

處理同享變量成績計劃三
 class MyRunner3 implements Runnable
 {
     public ThreadLocal<Integer> tl = new ThreadLocal<Integer>();

     public void run()
     {
         System.out.println(Thread.currentThread().getName() + " Start.");
         for (int i = 0; i <= 100; i++)
         {
             if (tl.get() == null)
             {
                 tl.set(new Integer(0));
             }
             int sum = ((Integer)tl.get()).intValue();
             sum+= i;
             tl.set(new Integer(sum));
             try {
                 Thread.sleep(10);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }

         System.out.println(Thread.currentThread().getName() + " --- The value of sum is " + ((Integer)tl.get()).intValue());
         System.out.println(Thread.currentThread().getName() + " End.");
     }
 }

 
 private static void sharedVaribleTest4() throws InterruptedException
 {
     MyRunner3 runner = new MyRunner3();
     Thread thread1 = new Thread(runner);
     Thread thread2 = new Thread(runner);
     thread1.setDaemon(true);
     thread2.setDaemon(true);
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
 }

綜上三種計劃,第一種計劃會下降多線程履行的效力,是以,我們推舉應用第二種或許第三種計劃。

掌握履行步調

說到履行步調,我們可使用synchronized症結字來處理它。

履行步調成績處理計劃
 class MySyncRunner implements Runnable
 {
     public void run() {
         synchronized(this)
         {
             System.out.println(Thread.currentThread().getName() + " Start.");
             for(int i = 1; i <= 5; i++)
             {
                 System.out.println(Thread.currentThread().getName() + " Running step " + i);
                 try
                 {
                     Thread.sleep(50);
                 }
                 catch(InterruptedException ex)
                 {
                     ex.printStackTrace();
                 }
             }
             System.out.println(Thread.currentThread().getName() + " End.");
         }
     }
 }

 
 private static void syncTest2() throws InterruptedException
 {
     MySyncRunner runner = new MySyncRunner();
     Thread thread1 = new Thread(runner);
     Thread thread2 = new Thread(runner);
     thread1.setDaemon(true);
     thread2.setDaemon(true);
     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
 }

在線程同步的話題上,synchronized是一個異常主要的症結字。它的道理和數據庫中事務鎖的道理相似。我們在應用進程中,應當盡可能縮減synchronized籠罩的規模,緣由有二:1)被它籠罩的規模是串行的,效力低;2)輕易發生逝世鎖。我們來看上面的示例:

synchronized示例
 private static void syncTest3() throws InterruptedException
 {
     final List<Integer> list = new ArrayList<Integer>();

     Thread thread1 = new Thread()
     {
         public void run()
         {
             System.out.println(Thread.currentThread().getName() + " Start.");
             Random r = new Random(100);
             synchronized(list)
             {
                 for (int i = 0; i < 5; i++)
                 {
                     list.add(new Integer(r.nextInt()));
                 }
                 System.out.println("The size of list is " + list.size());
             }
             try
             {
                 Thread.sleep(500);
             }
             catch(InterruptedException ex)
             {
                 ex.printStackTrace();
             }
             System.out.println(Thread.currentThread().getName() + " End.");
         }
     };

     Thread thread2 = new Thread()
     {
         public void run()
         {
             System.out.println(Thread.currentThread().getName() + " Start.");
             Random r = new Random(100);
             synchronized(list)
             {
                 for (int i = 0; i < 5; i++)
                 {
                     list.add(new Integer(r.nextInt()));
                 }
                 System.out.println("The size of list is " + list.size());
             }
             try
             {
                 Thread.sleep(500);
             }
             catch(InterruptedException ex)
             {
                 ex.printStackTrace();
             }
             System.out.println(Thread.currentThread().getName() + " End.");
         }
     };

     thread1.start();
     thread2.start();
     thread1.join();
     thread2.join();
 }

我們應當把須要同步的內容集中在一路,盡可能不包括其他不相干的、消費年夜量資本的操作,示例中線程休眠的操作明顯不該該包含在外面。

結構線程池

我們在<基於Java回想之收集通訊的運用剖析>中,曾經構建了一個Socket銜接池,這裡我們在此基本上,構建一個線程池,完成根本的啟動、休眠、叫醒、停滯操作。

根本思緒照樣以數組的情勢堅持一系列線程,經由過程Socket通訊,客戶端向辦事器端發送死令,當辦事器端吸收到敕令後,依據收到的敕令對線程數組中的線程停止操作。

Socket客戶真個代碼堅持不變,仍然采取構建Socket銜接池時的代碼,我們重要針對辦事器端停止改革。

起首,我們須要界說一個線程對象,它用來履行我們的營業操作,這裡簡化起見,只讓線程停止休眠。

界說線程對象
 enum ThreadStatus
 {
     Initial,
     Running,
     Sleeping,
     Stopped
 }

 enum ThreadTask
 {
     Start,
     Stop,
     Sleep,
     Wakeup
 }

 
 class MyThread extends Thread
 {
     public ThreadStatus status = ThreadStatus.Initial;
     public ThreadTask task;
     public void run()
     {
         status = ThreadStatus.Running;
         while(true)
         {
             try {
                 Thread.sleep(3000);
                 if (status == ThreadStatus.Sleeping)
                 {
                     System.out.println(Thread.currentThread().getName() + " 進入休眠狀況。");
                     this.wait();
                 }
             } catch (InterruptedException e) {
                 System.out.println(Thread.currentThread().getName() + " 運轉進程中湧現毛病。");
                 status = ThreadStatus.Stopped;
             }
         }
     }
 }

然後,我們須要界說一個線程治理器,它用來對線程池中的線程停止治理,代碼以下:

界說線程池治理對象
 class MyThreadManager
 {
     public static void manageThread(MyThread[] threads, ThreadTask task)
     {
         for (int i = 0; i < threads.length; i++)
         {
             synchronized(threads[i])
             {
                 manageThread(threads[i], task);
             }
         }
         System.out.println(getThreadStatus(threads));
     }

     public static void manageThread(MyThread thread, ThreadTask task)
     {
         if (task == ThreadTask.Start)
         {
             if (thread.status == ThreadStatus.Running)
             {
                 return;
             }
             if (thread.status == ThreadStatus.Stopped)
             {
                 thread = new MyThread();
             }
             thread.status = ThreadStatus.Running;
             thread.start();

         }
         else if (task == ThreadTask.Stop)
         {
             if (thread.status != ThreadStatus.Stopped)
             {
                 thread.interrupt();
                 thread.status = ThreadStatus.Stopped;
             }
         }
         else if (task == ThreadTask.Sleep)
         {
             thread.status = ThreadStatus.Sleeping;
         }
         else if (task == ThreadTask.Wakeup)
         {
             thread.notify();
             thread.status = ThreadStatus.Running;
         }
     }

     public static String getThreadStatus(MyThread[] threads)
     {
         StringBuffer sb = new StringBuffer();
         for (int i = 0; i < threads.length; i++)
         {
             sb.append(threads[i].getName() + "的狀況:" + threads[i].status).append("\r\n");
         }
         return sb.toString();
     }
 }

最初,是我們的辦事器端,它赓續接收客戶真個要求,每收到一個銜接要求,辦事器端會新開一個線程,來處置後續客戶端發來的各類操作指令。

界說辦事器端線程池對象
 public class MyThreadPool {

     public static void main(String[] args) throws IOException
     {
         MyThreadPool pool = new MyThreadPool(5);
     }

     private int threadCount;
     private MyThread[] threads = null;

    
     public MyThreadPool(int count) throws IOException
     {
         this.threadCount = count;
         threads = new MyThread[count];
         for (int i = 0; i < threads.length; i++)
         {
             threads[i] = new MyThread();
             threads[i].start();
         }
         Init();
     }

     private void Init() throws IOException
     {
         ServerSocket serverSocket = new ServerSocket(5678);
         while(true)
         {
             final Socket socket = serverSocket.accept();
             Thread thread = new Thread()
             {
                 public void run()
                 {
                     try
                     {
                         System.out.println("檢測到一個新的Socket銜接。");
                         BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                         PrintStream ps = new PrintStream(socket.getOutputStream());
                         String line = null;
                         while((line = br.readLine()) != null)
                         {
                             System.out.println(line);
                             if (line.equals("Count"))
                             {
                                 System.out.println("線程池中有5個線程");
                             }
                             else if (line.equals("Status"))
                             {
                                 String status = MyThreadManager.getThreadStatus(threads);
                                 System.out.println(status);
                             }
                             else if (line.equals("StartAll"))
                             {
                                 MyThreadManager.manageThread(threads, ThreadTask.Start);
                             }
                             else if (line.equals("StopAll"))
                             {
                                 MyThreadManager.manageThread(threads, ThreadTask.Stop);
                             }
                             else if (line.equals("SleepAll"))
                             {
                                 MyThreadManager.manageThread(threads, ThreadTask.Sleep);
                             }
                             else if (line.equals("WakeupAll"))
                             {
                                 MyThreadManager.manageThread(threads, ThreadTask.Wakeup);
                             }
                             else if (line.equals("End"))
                             {
                                 break;
                             }
                             else
                             {
                                 System.out.println("Command:" + line);
                             }
                             ps.println("OK");
                             ps.flush();
                         }
                     }
                     catch(Exception ex)
                     {
                         ex.printStackTrace();
                     }
                 }
             };
             thread.start();
         }
     }
 }

摸索JDK中的concurrent對象包

為了簡化開辟人員在停止多線程開辟時的任務量,並削減法式中的bug,JDK供給了一套concurrent對象包,我們可以用它來便利的開辟多線程法式。
線程池

我們在下面完成了一個異常“粗陋”的線程池,concurrent對象包中也供給了線程池,並且應用異常便利。

concurrent對象包中的線程池分為3類:ScheduledThreadPool、FixedThreadPool和CachedThreadPool。

起首我們來界說一個Runnable的對象

界說Runnable對象
 class MyRunner implements Runnable
 {
     public void run() {
         System.out.println(Thread.currentThread().getName() + "運轉開端");
         for(int i = 0; i < 1; i++)
         {
             try
             {
                 System.out.println(Thread.currentThread().getName() + "正在運轉");
                 Thread.sleep(200);
             }
             catch(Exception ex)
             {
                 ex.printStackTrace();
             }
         }
         System.out.println(Thread.currentThread().getName() + "運轉停止");
     }
 }

可以看出,它的功效異常簡略,只是輸入了線程的履行進程。

ScheduledThreadPool

這和我們日常平凡應用的ScheduledTask比擬相似,或許說很像Timer,它可使得一個線程在指定的一段時光內開端運轉,而且在距離別的一段時光後再次運轉,直到線程池封閉。

示例代碼以下:

ScheduledThreadPool示例
 private static void scheduledThreadPoolTest()
 {
     final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);

     MyRunner runner = new MyRunner();

     final ScheduledFuture<?> handler1 = scheduler.scheduleAtFixedRate(runner, 1, 10, TimeUnit.SECONDS);
     final ScheduledFuture<?> handler2 = scheduler.scheduleWithFixedDelay(runner, 2, 10, TimeUnit.SECONDS);

     scheduler.schedule(new Runnable()
     {
         public void run()
         {
             handler1.cancel(true);
             handler2.cancel(true);
             scheduler.shutdown();
         }
     }, 30, TimeUnit.SECONDS
     );
 }

FixedThreadPool

這是一個指定容量的線程池,即我們可以指定在統一時光,線程池中最多有多個線程在運轉,超越的線程,須要等線程池中有余暇線程時,能力無機會運轉。

來看上面的代碼:

FixedThreadPool示例
 private static void fixedThreadPoolTest()
 {
     ExecutorService exec = Executors.newFixedThreadPool(3);
     for(int i = 0; i < 5; i++)
     {
         MyRunner runner = new MyRunner();
         exec.execute(runner);
     }
     exec.shutdown();
 }

留意它的輸入成果:

pool-1-thread-1運轉開端
pool-1-thread-1正在運轉
pool-1-thread-2運轉開端
pool-1-thread-2正在運轉
pool-1-thread-3運轉開端
pool-1-thread-3正在運轉
pool-1-thread-1運轉停止
pool-1-thread-1運轉開端
pool-1-thread-1正在運轉
pool-1-thread-2運轉停止
pool-1-thread-2運轉開端
pool-1-thread-2正在運轉
pool-1-thread-3運轉停止
pool-1-thread-1運轉停止
pool-1-thread-2運轉停止

可以看到從始至終,最多有3個線程在同時運轉。
CachedThreadPool

這是別的一種線程池,它不須要指定容量,只需有須要,它就會創立新的線程。

它的應用方法和FixedThreadPool異常像,來看上面的代碼:

CachedThreadPool示例
 private static void cachedThreadPoolTest()
 {
     ExecutorService exec = Executors.newCachedThreadPool();
     for(int i = 0; i < 5; i++)
     {
         MyRunner runner = new MyRunner();
         exec.execute(runner);
     }
     exec.shutdown();
 }

它的履行成果以下:

pool-1-thread-1運轉開端
pool-1-thread-1正在運轉
pool-1-thread-2運轉開端
pool-1-thread-2正在運轉
pool-1-thread-3運轉開端
pool-1-thread-3正在運轉
pool-1-thread-4運轉開端
pool-1-thread-4正在運轉
pool-1-thread-5運轉開端
pool-1-thread-5正在運轉
pool-1-thread-1運轉停止
pool-1-thread-2運轉停止
pool-1-thread-3運轉停止
pool-1-thread-4運轉停止
pool-1-thread-5運轉停止

可以看到,它創立了5個線程。
處置線程前往值

在有些情形下,我們須要應用線程的前往值,在上述的一切代碼中,線程這是履行了某些操作,沒有任何前往值。

若何做到這一點呢?我們可使用JDK中的Callable<T>和CompletionService<T>,前者前往單個線程的成果,後者前往一組線程的成果。
前往單個線程的成果

照樣直接看代碼吧:

Callable示例
 private static void callableTest() throws InterruptedException, ExecutionException
 {
     ExecutorService exec = Executors.newFixedThreadPool(1);
     Callable<String> call = new Callable<String>()
     {
         public String call()
         {
             return "Hello World.";
         }
     };
     Future<String> result = exec.submit(call);
     System.out.println("線程的前往值是" + result.get());
     exec.shutdown();
 }

履行成果以下:

線程的前往值是Hello World.

前往線程池中每一個線程的成果

這裡須要應用CompletionService<T>,代碼以下:

CompletionService示例
 private static void completionServiceTest() throws InterruptedException, ExecutionException
 {
     ExecutorService exec = Executors.newFixedThreadPool(10);
     CompletionService<String> service = new ExecutorCompletionService<String>(exec);
     for (int i = 0; i < 10; i++)
     {
         Callable<String> call = new Callable<String>()
         {
             public String call() throws InterruptedException
             {
                 return Thread.currentThread().getName();
             }
         };
         service.submit(call);
     }

     Thread.sleep(1000);
     for(int i = 0; i < 10; i++)
     {
         Future<String> result = service.take();
         System.out.println("線程的前往值是" + result.get());
     }
     exec.shutdown();
 }

履行成果以下:

線程的前往值是pool-2-thread-1
線程的前往值是pool-2-thread-2
線程的前往值是pool-2-thread-3
線程的前往值是pool-2-thread-5
線程的前往值是pool-2-thread-4
線程的前往值是pool-2-thread-6
線程的前往值是pool-2-thread-8
線程的前往值是pool-2-thread-7
線程的前往值是pool-2-thread-9
線程的前往值是pool-2-thread-10

完成臨盆者-花費者模子

關於臨盆者-花費者模子來講,我們應當都不會生疏,平日我們都邑應用某種數據構造來完成它。在concurrent對象包中,我們可使用BlockingQueue來完成臨盆者-花費者模子,以下:

BlockingQueue示例
 public class BlockingQueueSample {

     public static void main(String[] args)
     {
         blockingQueueTest();
     }

     private static void blockingQueueTest()
     {
         final BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
         final int maxSleepTimeForSetter = 10;
         final int maxSleepTimerForGetter = 10;

         Runnable setter = new Runnable()
         {
             public void run()
             {
                 Random r = new Random();
                 while(true)
                 {
                     int value = r.nextInt(100);
                     try
                     {
                         queue.put(new Integer(value));
                         System.out.println(Thread.currentThread().getName() + "---向隊列中拔出值" + value);
                         Thread.sleep(r.nextInt(maxSleepTimeForSetter) * 1000);
                     }
                     catch(Exception ex)
                     {
                         ex.printStackTrace();
                     }
                 }
             }
         };

         Runnable getter = new Runnable()
         {
             public void run()
             {
                 Random r = new Random();
                 while(true)
                 {
                     try
                     {
                         if (queue.size() == 0)
                         {
                             System.out.println(Thread.currentThread().getName() + "---隊列為空");
                         }
                         else
                         {
                             int value = queue.take().intValue();
                             System.out.println(Thread.currentThread().getName() + "---從隊列中獲得值" + value);
                         }
                         Thread.sleep(r.nextInt(maxSleepTimerForGetter) * 1000);
                     }
                     catch(Exception ex)
                     {
                         ex.printStackTrace();
                     }
                 }
             }
         };

         ExecutorService exec = Executors.newFixedThreadPool(2);
         exec.execute(setter);
         exec.execute(getter);
     }
 }

我們界說了兩個線程,一個線程向Queue中添加數據,一個線程從Queue中取數據。我們可以經由過程掌握maxSleepTimeForSetter和maxSleepTimerForGetter的值,來使得法式得出分歧的成果。

能夠的履行成果以下:

pool-1-thread-1---向隊列中拔出值88
pool-1-thread-2---從隊列中獲得值88
pool-1-thread-1---向隊列中拔出值75
pool-1-thread-2---從隊列中獲得值75
pool-1-thread-2---隊列為空
pool-1-thread-2---隊列為空
pool-1-thread-2---隊列為空
pool-1-thread-1---向隊列中拔出值50
pool-1-thread-2---從隊列中獲得值50
pool-1-thread-2---隊列為空
pool-1-thread-2---隊列為空
pool-1-thread-2---隊列為空
pool-1-thread-2---隊列為空
pool-1-thread-2---隊列為空
pool-1-thread-1---向隊列中拔出值51
pool-1-thread-1---向隊列中拔出值92
pool-1-thread-2---從隊列中獲得值51
pool-1-thread-2---從隊列中獲得值92

由於Queue中的值和Thread的休眠時光都是隨機的,所以履行成果也不是固定的。

應用旌旗燈號量來掌握線程

JDK供給了Semaphore來完成“旌旗燈號量”的功效,它供給了兩個辦法分離用於獲得和釋放旌旗燈號量:acquire和release,示例代碼以下:

SemaPhore示例
 private static void semaphoreTest()
 {
     ExecutorService exec = Executors.newFixedThreadPool(10);
     final Semaphore semp = new Semaphore(2);

     for (int i = 0; i < 10; i++)
     {
         Runnable runner = new Runnable()
         {
             public void run()
             {
                 try
                 {
                     semp.acquire();
                     System.out.println(new Date() + " " + Thread.currentThread().getName() + "正在履行。");
                     Thread.sleep(5000);
                     semp.release();
                 }
                 catch(Exception ex)
                 {
                     ex.printStackTrace();
                 }
             }
         };
         exec.execute(runner);
     }

     exec.shutdown();
 }

履行成果以下:

Tue May 07 11:22:11 CST 2013 pool-1-thread-1正在履行。
Tue May 07 11:22:11 CST 2013 pool-1-thread-2正在履行。
Tue May 07 11:22:17 CST 2013 pool-1-thread-3正在履行。
Tue May 07 11:22:17 CST 2013 pool-1-thread-4正在履行。
Tue May 07 11:22:22 CST 2013 pool-1-thread-5正在履行。
Tue May 07 11:22:22 CST 2013 pool-1-thread-6正在履行。
Tue May 07 11:22:27 CST 2013 pool-1-thread-7正在履行。
Tue May 07 11:22:27 CST 2013 pool-1-thread-8正在履行。
Tue May 07 11:22:32 CST 2013 pool-1-thread-10正在履行。
Tue May 07 11:22:32 CST 2013 pool-1-thread-9正在履行。

可以看出,雖然線程池中創立了10個線程,然則同時運轉的,只要2個線程。
掌握線程池中一切線程的履行步調

在後面,我們曾經提到,可以用synchronized症結字來掌握單個線程中的履行步調,那末假如我們想要對線程池中的一切線程的履行步調停止掌握的話,應當若何完成呢?

我們有兩種方法,一種是應用CyclicBarrier,一種是應用CountDownLatch。

CyclicBarrier應用了相似於Object.wait的機制,它的結構函數中須要吸收一個整型數字,用來講明它須要掌握的線程數量,當在線程的run辦法中挪用它的await辦法時,它會包管一切的線程都履行到這一步,才會持續履行前面的步調。

示例代碼以下:

CyclicBarrier示例
 class MyRunner2 implements Runnable
 {
     private CyclicBarrier barrier = null;
     public MyRunner2(CyclicBarrier barrier)
     {
         this.barrier = barrier;
     }

     public void run() {
         Random r = new Random();
         try
         {
             for (int i = 0; i < 3; i++)
             {
                 Thread.sleep(r.nextInt(10) * 1000);
                 System.out.println(new Date() + "--" + Thread.currentThread().getName() + "--第" + (i + 1) + "次期待。");
                 barrier.await();
             }
         }
         catch(Exception ex)
         {
             ex.printStackTrace();
         }
     }

 }

 private static void cyclicBarrierTest()
 {
     CyclicBarrier barrier = new CyclicBarrier(3);

     ExecutorService exec = Executors.newFixedThreadPool(3);
     for (int i = 0; i < 3; i++)
     {
         exec.execute(new MyRunner2(barrier));
     }
     exec.shutdown();
 }

履行成果以下:

Tue May 07 11:31:20 CST 2013--pool-1-thread-2--第1次期待。
Tue May 07 11:31:21 CST 2013--pool-1-thread-3--第1次期待。
Tue May 07 11:31:24 CST 2013--pool-1-thread-1--第1次期待。
Tue May 07 11:31:24 CST 2013--pool-1-thread-1--第2次期待。
Tue May 07 11:31:26 CST 2013--pool-1-thread-3--第2次期待。
Tue May 07 11:31:30 CST 2013--pool-1-thread-2--第2次期待。
Tue May 07 11:31:32 CST 2013--pool-1-thread-1--第3次期待。
Tue May 07 11:31:33 CST 2013--pool-1-thread-3--第3次期待。
Tue May 07 11:31:33 CST 2013--pool-1-thread-2--第3次期待。

可以看出,thread-2到第1次期待點時,一向比及thread-1達到後才持續履行。

CountDownLatch則是采用相似”倒計時計數器”的機制來掌握線程池中的線程,它有CountDown和Await兩個辦法。示例代碼以下:

CountDownLatch示例
 private static void countdownLatchTest() throws InterruptedException
 {
     final CountDownLatch begin = new CountDownLatch(1);
     final CountDownLatch end = new CountDownLatch(5);
     ExecutorService exec = Executors.newFixedThreadPool(5);
     for (int i = 0; i < 5; i++)
     {
         Runnable runner = new Runnable()
         {
             public void run()
             {
                 Random r = new Random();
                 try
                 {
                     begin.await();
                     System.out.println(Thread.currentThread().getName() + "運轉開端");
                     Thread.sleep(r.nextInt(10)*1000);
                     System.out.println(Thread.currentThread().getName() + "運轉停止");
                 }
                 catch(Exception ex)
                 {
                     ex.printStackTrace();
                 }
                 finally
                 {
                     end.countDown();
                 }
             }
         };
         exec.execute(runner);
     }
     begin.countDown();
     end.await();
     System.out.println(Thread.currentThread().getName() + "運轉停止");
     exec.shutdown();
 }

履行成果以下:

pool-1-thread-1運轉開端
pool-1-thread-5運轉開端
pool-1-thread-2運轉開端
pool-1-thread-3運轉開端
pool-1-thread-4運轉開端
pool-1-thread-2運轉停止
pool-1-thread-1運轉停止
pool-1-thread-3運轉停止
pool-1-thread-5運轉停止
pool-1-thread-4運轉停止
main運轉停止

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved