自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

多線程的批量線程同步解決方案

開發(fā) 后端
本文介紹多線程的批量線程同步解決方案。解決方法是,創(chuàng)建一個(gè)鎖對(duì)象,該鎖對(duì)象提供一個(gè)當(dāng)前線程等待其他線程的方法。

多線程運(yùn)行時(shí)有待處理線程?試試看下面介紹的這個(gè)批量線程同步方法吧。

在一批線程處理程序中,有時(shí)必須等到所有線程全部運(yùn)行完后,才能進(jìn)行下一步任務(wù)處理, 可以采用如下方法解決,創(chuàng)建一個(gè)鎖對(duì)象 ,該鎖對(duì)象提供一個(gè)當(dāng)前線程等待其他線程的方法。見代碼:

  1. /**  
  2.  *   
  3.  * 此類主要用來處理線程的同步屏蔽模型,比如,一批線程運(yùn)行,必須在最后一個(gè)線程運(yùn)行  
  4.  * 完后,才能進(jìn)行下一步的操作,那么就可以創(chuàng)建一個(gè)鎖對(duì)象,鎖對(duì)象提供一個(gè)線程等待其他線程  
  5.  * 的方法,如果當(dāng)前線程運(yùn)行時(shí),還有未運(yùn)行的線程,則此線程wait,否則,此線程喚醒其他阻塞的  
  6.  * 線程,進(jìn)而最終完成線程的運(yùn)行  
  7.  * */ 
  8. public class LockObject {  
  9.  
  10.  private int totalThread = 0;  
  11.  private int currentThread = 0;  
  12.  
  13.  public LockObject(int totalThread) {  
  14.   this.totalThread = totalThread;  
  15.   this.currentThread = 1;  
  16.  }  
  17.  
  18.  public synchronized void waitForOtherThread() {  
  19.   if (this.currentThread < this.totalThread) {  
  20.    this.currentThread++;  
  21.    try {  
  22.     this.wait();  
  23.    } catch (InterruptedException e) {  
  24.     // TODO Auto-generated catch block  
  25.     e.printStackTrace();  
  26.    }  
  27.   } else {  
  28.    this.currentThread = 1;  
  29.    notifyAll();  
  30.   }  
  31.  }  
  32.  
  33.  public int getTotalThread() {  
  34.   return totalThread;  
  35.  }  
  36.  
  37.  public void setTotalThread(int totalThread) {  
  38.   this.totalThread = totalThread;  
  39.  }  
  40.  
  41.  public int getCurrentThread() {  
  42.   return currentThread;  
  43.  }  
  44.  
  45.  public void setCurrentThread(int currentThread) {  
  46.   this.currentThread = currentThread;  
  47.  }  
  48. }  
  49.  

批量線程同步機(jī)制介紹

此對(duì)象提供 二個(gè)私有變量,totalThread 的初始值為所運(yùn)行的線程的總數(shù),currentThread 為當(dāng)前正在運(yùn)行的線程數(shù)。

線程運(yùn)行時(shí)處理完自己的任務(wù)后調(diào)用方法waitForOtherThread 等待其他線程結(jié)束,即當(dāng)前運(yùn)行線程數(shù)與線程總數(shù)的比較

如果運(yùn)行線程數(shù)小于線程總數(shù),則當(dāng)前運(yùn)行線程數(shù)+1 后,當(dāng)前線程進(jìn)入等待狀態(tài),否則,喚醒其他等待線程。

見測(cè)試程序

  1. public class MyThread extends Thread {  
  2.  public static LockObject lo = new LockObject(1000);  
  3.  
  4.  public MyThread(String threadName) {  
  5.   super(threadName);  
  6.  }  
  7.  
  8.  public void run() {  
  9.    System.out.println(Thread.currentThread().getName() + " ----開始運(yùn)行");  
  10.    lo.waitForOtherThread();  
  11.    System.out.println(Thread.currentThread().getName() + " ----結(jié)束運(yùn)行");  
  12.  }  
  13.  
  14.  public static void main(String[] args) {  
  15.   for (int i = 1; i <= 1000; i++) {  
  16.    Thread thread = new MyThread("第" + i + "個(gè)線程");  
  17.    thread.setPriority(NORM_PRIORITY);  
  18.    thread.start();  
  19.   }  
  20.  }  
  21.  
  22. }  

以上就介紹了批量線程同步的實(shí)現(xiàn)。

【編輯推薦】

  1. C#線程同步詳細(xì)分析
  2. 運(yùn)用C#數(shù)據(jù)提供者
  3. C#允許運(yùn)算符重載剖析
  4. Java和C#頂層聲明概述
  5. C#完全限定名簡(jiǎn)單分析
責(zé)任編輯:yangsai 來源: JavaEye博客
相關(guān)推薦

2025-03-03 01:25:00

SpringAOP日志

2009-07-15 17:09:32

Swing線程

2012-05-18 11:17:58

Java多線程

2010-01-21 11:27:30

linux多線程機(jī)制線程同步

2025-01-07 08:20:00

2015-07-22 09:51:51

iOS開發(fā)線程

2013-07-16 12:13:27

iOS多線程多線程概念GCD

2015-07-22 09:39:38

IOS多線程同步

2009-03-18 09:26:23

Winform多線程C#

2011-06-22 13:47:16

Java多線程

2011-06-22 13:57:54

Java多線程

2010-01-21 11:23:49

Linux多線程同步消息隊(duì)列

2021-05-17 07:51:44

SimpleDateF線程安全

2011-04-14 13:27:53

Synchronize多線程

2010-01-21 11:22:35

Linux多線程同步

2010-03-15 19:37:00

Java多線程同步

2009-03-24 08:56:23

數(shù)據(jù)同步多線程Java

2009-07-01 17:34:03

Servlet和JSP

2015-09-10 09:30:54

Java多線程同步

2022-04-07 07:40:40

線程安全變量
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)