1.阻塞隊列: BlockingQueue(接口)
重要方法:take(),put(),當爲空,或者容量已滿,自動進入阻塞狀態。
主要實現類:ArrayBlockingQueue,LinkBlockingQueue,SynchronousQueue
2.鎖類
接口:Lock,ReadWriteLock(讀寫鎖)。
實現類:ReentrantLock,ReentrantReadWriteLock。
重要方法:lock(),unlock(),readLock().lock(),readLock().unlock(),
線程通信(類型wait(),notify()):lock.newCondition();condition.await();condition.signal();
3.線程池
ExecutorService threadPool = Executors.newFixedThreadPool(3);
ExecutorService threadPool = Executors.newCachedThreadPool();
ExecutorService threadPool = Executors.newSingleThreadExecutor();
<span style="white-space:pre"> threadPool.execute(new Runnable(){
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void run() {
<span style="white-space:pre"> </span>for(int j=1;j<=10;j++){
<span style="white-space:pre"> </span>try {
<span style="white-space:pre"> </span>Thread.sleep(20);
<span style="white-space:pre"> </span>} catch (InterruptedException e) {
<span style="white-space:pre"> </span>// TODO Auto-generated catch block
<span style="white-space:pre"> </span>e.printStackTrace();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for task of " + task);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>});</span>
4.同步工具類:
倒計時:CountDownLatch
信號:Semaphore
CyclicBarrier