生活随笔
收集整理的這篇文章主要介紹了
两个小程序大概的了解一下java的线程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、java的notify與wait
package org.calonlan.soulpower;public class MyThreadTest implements Runnable {private String name;private Object prev;private Object self;public MyThreadTest(String name, Object prev, Object self) {super();this.name = name;this.prev = prev;this.self = self;}@Overridepublic void run() {int count = 10;while (count > 0) {synchronized (prev) {//請求獲得上一個的鎖synchronized (self) {//請求獲得自己的鎖System.out.println(name+"--"+count);count--;try {Thread.sleep(1);//這里測試sleep,sleep只會讓當前線程讓出cpu、內存等資源,但不會釋放鎖} catch (InterruptedException e) {e.printStackTrace();}self.notify();//通知等待sefl的鎖的線程可以啟動了,同時在synchronized (self)塊執行完成后釋放self鎖,程序繼續執行}try {prev.wait();//這里釋放prev鎖,并且程序進入阻塞狀態。等在notify的時候重新啟動程序執行} catch (InterruptedException e) {e.printStackTrace();}}}}public static void main(String[] args) throws Exception {Object a = new Object();Object b = new Object();Object c = new Object();MyThreadTest ta = new MyThreadTest("A", c, a);MyThreadTest tb = new MyThreadTest("B", a, b);MyThreadTest tc = new MyThreadTest("C", b, c);new Thread(ta).start();Thread.sleep(10);//---------{new Thread(tb).start();// | Thread.sleep(10);//---------{三個Thread.sleep()是讓主線程中保證三個線程按順序啟動new Thread(tc).start();// |Thread.sleep(10);//---------{}}
二、java的condition中的signal與await
package org.calonlan.soulpower;import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class BoundedBufferTest {final Lock lock = new ReentrantLock();//定義一個鎖final Condition notFull = lock.newCondition();//鎖中的狀態,用來標識notfullfinal Condition notEmpty = lock.newCondition();//鎖中的狀態,用來標識notemptyfinal Object[] items = new Object[100];//隊列int putptr, takeptr, count;public void put(Object x) throws Exception {lock.lock();/*在lock與unlock之間的代碼才能保證線程安全*/try {// condition需要在while中判斷while (count == items.length)notFull.await();//隊列滿的了時候用notFull來阻塞程序/*接收到notFull的signal或者隊列本來就不滿執行下面的代碼*/items[putptr] = x;if (++putptr == items.length)putptr = 0;++count;notEmpty.signal();} finally {/*在lock與unlock之間的代碼才能保證線程安全*/lock.unlock();}}public Object take() throws Exception {lock.lock();/*在lock與unlock之間的代碼才能保證線程安全*/try {while (count == 0)notEmpty.await();//使用notemty來阻塞線程(如果count==0的時候),這里一定要用while來做判斷Object x = items[takeptr];if (++takeptr == items.length)takeptr = 0;--count;notFull.signal();return x;} finally {/*在lock與unlock之間的代碼才能保證線程安全*/lock.unlock();}}static class Thread1 extends Thread {private BoundedBufferTest BoundedBufferTest;public Thread1(BoundedBufferTest boundedBufferTest) {super();BoundedBufferTest = boundedBufferTest;}@Overridepublic void run() {int count = 0;try {while (true) {System.out.println(count + "放入");BoundedBufferTest.put(count++);Thread.sleep(100);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}static class Thread2 extends Thread {private BoundedBufferTest BoundedBufferTest;public Thread2(BoundedBufferTest boundedBufferTest) {super();BoundedBufferTest = boundedBufferTest;}@Overridepublic void run() {try {while (true) {System.out.println(Thread.currentThread().getName()+ BoundedBufferTest.take());Thread.sleep(10000);}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {BoundedBufferTest boundedBufferTest = new BoundedBufferTest();Thread2 thread2 = new Thread2(boundedBufferTest);Thread1 thread1 = new Thread1(boundedBufferTest);thread1.start();thread2.start();}
}
總結
以上是生活随笔為你收集整理的两个小程序大概的了解一下java的线程的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。