大体意思就是一个是给汽车涂蜡( waxon )
一个是给汽车蜡抛光(waxoff)
必须涂蜡完后才能抛光
抛光玩后才能涂蜡
现在有一个涂蜡的
2个抛蜡的(2者同时只能有一个)
下面是代码 实现不了import java.util.concurrent.*;
public class Car {
boolean b = false;
synchronized void waxon()
{
System.out.println ("waxon");
}
synchronized void waxoff()
{
System.out.println ("waxoff");
}
String str = new String(" ");
class WaxOn implements Runnable
{
public void run()
{
while(true)
{
try
{
synchronized( str )
{
if(b==true)
{
str.wait();
}
waxon();
b=true;
str.notify();
}
}
catch(Exception e){}
}
}
}
class WaxOff implements Runnable
{
public void run()
{
while(true)
{
try
{
synchronized (str)
{
if(b==false)
str.wait();
waxoff();
b=false;
str.notify();
}
}
catch(Exception e){}
}
}
}
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
Car c = new Car();
es.execute(c.new WaxOn());
es.execute(c.new WaxOff());
es.execute(c.new WaxOff());
}
}为什么不能实现2个waxoff线程的同步
允许结果是出现了
waxon
waxoff
waxoff
waxon
....
我希望的是
waxon
waxoff
waxon
waxoff
.....
要怎样改啊
我加了一个String str1 = new String(" ");
//将2个run()函数里的str该为str1
synchronized( str )
{
if(b==true)
{
str1.wait();
}
waxon();
b=true;
str1.notify();
}就能实现这个效果 但是我感觉没什么区别
却能够实现 为什么
一个是给汽车蜡抛光(waxoff)
必须涂蜡完后才能抛光
抛光玩后才能涂蜡
现在有一个涂蜡的
2个抛蜡的(2者同时只能有一个)
下面是代码 实现不了import java.util.concurrent.*;
public class Car {
boolean b = false;
synchronized void waxon()
{
System.out.println ("waxon");
}
synchronized void waxoff()
{
System.out.println ("waxoff");
}
String str = new String(" ");
class WaxOn implements Runnable
{
public void run()
{
while(true)
{
try
{
synchronized( str )
{
if(b==true)
{
str.wait();
}
waxon();
b=true;
str.notify();
}
}
catch(Exception e){}
}
}
}
class WaxOff implements Runnable
{
public void run()
{
while(true)
{
try
{
synchronized (str)
{
if(b==false)
str.wait();
waxoff();
b=false;
str.notify();
}
}
catch(Exception e){}
}
}
}
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
Car c = new Car();
es.execute(c.new WaxOn());
es.execute(c.new WaxOff());
es.execute(c.new WaxOff());
}
}为什么不能实现2个waxoff线程的同步
允许结果是出现了
waxon
waxoff
waxoff
waxon
....
我希望的是
waxon
waxoff
waxon
waxoff
.....
要怎样改啊
我加了一个String str1 = new String(" ");
//将2个run()函数里的str该为str1
synchronized( str )
{
if(b==true)
{
str1.wait();
}
waxon();
b=true;
str1.notify();
}就能实现这个效果 但是我感觉没什么区别
却能够实现 为什么
洗车线程先进入洗车,这时候两个打蜡线程都在等待。
洗车完毕后,一个打蜡线程开始工作,一个打蜡线程和一个洗车线程等待
打蜡完毕后,有可能是另一个打蜡线程抢占CPU,这时候,b==false,该线程wait,但注意,该线程已经进入同步区,
该打蜡线程挂起后,洗车和另一个打蜡线程都有可能抢占CPU,如果是另一个打蜡线程抢占CPU,则,第二个打蜡线程将会停在和第一个打蜡线程相同的位置,
这时候,洗车线程可以工作,洗车完毕后,两个打蜡线程都可以执行了,因为他们都不用再受synchronized限制。
public class Car {
boolean b = false; synchronized void waxon() {
System.out.println("waxon");
} synchronized void waxoff() {
System.out.println("waxoff");
} String str = new String(" "); class WaxOn implements Runnable {
public void run() {
while (true) {
try { if (b == true) {
str.wait();
}
synchronized (str) {
waxon();
b = true;
str.notify();
}
} catch (Exception e) {
}
}
}
} class WaxOff implements Runnable {
public void run() {
while (true) {
try { if (b == false)
str.wait();
waxoff();
synchronized (str) {
b = false;
str.notify();
}
} catch (Exception e) {
}
}
}
}
waxon
waxoff
waxon
waxoff
waxon
waxoff
waxon
waxoff
waxon
waxoff
waxon
waxoff