import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* sleep 不会释放锁
* wait 方法会释放锁的。所以如下演示中,会同时输出多个wait而sleep需要按照顺序输出
* 原因在于:Sleeper需要完成sleep后,运行完synchronized中的方法才能释放锁,下一个线程才能输出
* 而wait中,只要运行到wait,锁就会被释放,别的线程立即就能运行
* 此外,notify 和notifyAll也是不同的
* notify会从所有wait线程中选择一个进行启动,如果这个线程中还有判断逻辑,并且如果再次判断还要继续等待的话,那么所有的线程就彻底挂了
* notifyall则不同,会将所有的wait进行激活,需要注意的是,wait激活后并不是立即运行wait后的代码,而是所有的线程进行锁的竞争,
* 只有有锁的线程才能获得执行,该线程执行完后再由剩下的线程进行锁竞争,以此类推。所以演示代码中的wait会每隔一秒顺序输出
* 而notify不存在锁竞争的问题,因为只激活了一个线程,这个线程必然会获得锁,所以不存在竞争问题。
* @author wangjian
*
*/
class Sleeper{
public synchronized void sleepMom(){
System.out.println("准备Sleeper");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("完成Sleeper");
}
}
class SleepMyTask implements Runnable {
Sleeper sleeper;
int id=0;
SleepMyTask(Sleeper sleeper,int id){
this.sleeper=sleeper;
this.id=id;
}
public void run() {
System.out.println(id+"");
sleeper.sleepMom();
}
}
class Water{
public synchronized void waitMom() throws InterruptedException{
System.out.println("准备wait");
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TimeUnit.SECONDS.sleep(1);
System.out.print("完成Sleeper");
}
public synchronized void noticyone(){
notify();
}
public synchronized void noticyall(){
notifyAll();
}
}
class WaterMyTask implements Runnable {
Water water;
int id=0;
WaterMyTask(Water water,int id){
this.water=water;
this.id=id;
}
public void run() {
System.out.println(id+"");
try {
water.waitMom();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class SleepAndWait {
public static void main(String[] args) throws InterruptedException {
// Sleeper sleeper = new Sleeper();
ExecutorService exec = Executors.newCachedThreadPool();
// for(int i=0;i<10;i++){
// exec.execute(new SleepMyTask(sleeper,i));
// }
Water water = new Water();
for(int i=0;i<10;i++){
exec.execute(new WaterMyTask(water,i));
}
Thread.sleep(5000);
water.noticyone();
Thread.sleep(5000);
water.noticyall();
}
}
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* sleep 不会释放锁
* wait 方法会释放锁的。所以如下演示中,会同时输出多个wait而sleep需要按照顺序输出
* 原因在于:Sleeper需要完成sleep后,运行完synchronized中的方法才能释放锁,下一个线程才能输出
* 而wait中,只要运行到wait,锁就会被释放,别的线程立即就能运行
* 此外,notify 和notifyAll也是不同的
* notify会从所有wait线程中选择一个进行启动,如果这个线程中还有判断逻辑,并且如果再次判断还要继续等待的话,那么所有的线程就彻底挂了
* notifyall则不同,会将所有的wait进行激活,需要注意的是,wait激活后并不是立即运行wait后的代码,而是所有的线程进行锁的竞争,
* 只有有锁的线程才能获得执行,该线程执行完后再由剩下的线程进行锁竞争,以此类推。所以演示代码中的wait会每隔一秒顺序输出
* 而notify不存在锁竞争的问题,因为只激活了一个线程,这个线程必然会获得锁,所以不存在竞争问题。
* @author wangjian
*
*/
class Sleeper{
public synchronized void sleepMom(){
System.out.println("准备Sleeper");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print("完成Sleeper");
}
}
class SleepMyTask implements Runnable {
Sleeper sleeper;
int id=0;
SleepMyTask(Sleeper sleeper,int id){
this.sleeper=sleeper;
this.id=id;
}
public void run() {
System.out.println(id+"");
sleeper.sleepMom();
}
}
class Water{
public synchronized void waitMom() throws InterruptedException{
System.out.println("准备wait");
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
TimeUnit.SECONDS.sleep(1);
System.out.print("完成Sleeper");
}
public synchronized void noticyone(){
notify();
}
public synchronized void noticyall(){
notifyAll();
}
}
class WaterMyTask implements Runnable {
Water water;
int id=0;
WaterMyTask(Water water,int id){
this.water=water;
this.id=id;
}
public void run() {
System.out.println(id+"");
try {
water.waitMom();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class SleepAndWait {
public static void main(String[] args) throws InterruptedException {
// Sleeper sleeper = new Sleeper();
ExecutorService exec = Executors.newCachedThreadPool();
// for(int i=0;i<10;i++){
// exec.execute(new SleepMyTask(sleeper,i));
// }
Water water = new Water();
for(int i=0;i<10;i++){
exec.execute(new WaterMyTask(water,i));
}
Thread.sleep(5000);
water.noticyone();
Thread.sleep(5000);
water.noticyall();
}
}