About the diffrence of wait timed_wait and block in java

Posted 进无止境

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了About the diffrence of wait timed_wait and block in java相关的知识,希望对你有一定的参考价值。

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_WAITING
		new Thread(target1, "1").start();
		Thread.sleep(200);
		// BLOCKED
		new Thread(target1, "2").start();

		// ----------------lock------------------
		// TIMED_WAITING
		new Thread(target2, "3").start();
		Thread.sleep(200);
		// WAITING
		new Thread(target2, "4").start();

	}

	public static synchronized void get() {

		try {
			Thread.sleep(5000 * 5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
	}

	public static void get2() {

		try {
			lock.lock();
			Thread.sleep(5000 * 5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			lock.unlock();
		}
	}

	static Runnable target1 = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			get();
		}
	};

	static Runnable target2 = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			get2();
		}
	};

}

 

以上是关于About the diffrence of wait timed_wait and block in java的主要内容,如果未能解决你的问题,请参考以下文章

论文阅读 | What Does BERT Learn about the Structure of Language?

hdu-1492 The number of divisors(约数) about Humble Numbers---因子数公式

Some features we need to keep in mind about the implementation of QuickSort algorithm

A log about Reading the memroy of Other Process in C++/WIN API--ReadProcessMemory()

These two days __ us think about the cleaning roles of mother and fathers.

[Mathematics][MIT 18.03] Proof of a Theory about the Solution to Second-order Linear Homogeneous Dif