標籤:todo tar ati stack gen out rup runnable str
import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * * @author daxin * */public class Main1 {static Lock lock = new ReentrantLock();public static void main(String[] args) throws Exception {// TIMED_WAITING// Thread.sleep(5000*5000);// ----------------synchronized---------------------// TIMED_WAITINGnew Thread(target1, "1").start();Thread.sleep(200);// BLOCKEDnew Thread(target1, "2").start();// ----------------lock------------------// TIMED_WAITINGnew Thread(target2, "3").start();Thread.sleep(200);// WAITINGnew Thread(target2, "4").start();}public static synchronized void get() {try {Thread.sleep(5000 * 5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {}}public static void get2() {try {lock.lock();Thread.sleep(5000 * 5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {lock.unlock();}}static Runnable target1 = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubget();}};static Runnable target2 = new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubget2();}};}
About the diffrence of wait timed_wait and block in java