import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MyThread005 {
public static void main(String[] args)
{
//创建线程池
ExecutorService exe = Executors.newFixedThreadPool(5);
//执行
TA ta = new TA();//创建操作对象ta
TR tb = new TR();//创建操作对象tb
Thread ta1 = new Thread(ta);
Thread ta2 = new Thread(ta);
Thread ta3 = new Thread(ta);
Thread tb1 = new Thread(tb);
Thread tb2 = new Thread(tb);
exe.execute(ta1);
exe.execute(ta2);
exe.execute(ta3);
exe.execute(tb1);
exe.execute(tb2);
//停止
exe.shutdown();
}
}
class TA extends Thread {
private String lock = "lock";//对象锁
private int apple = 30;
public void run()
{
synchronized (lock)//一直独占这个对象锁,也就是获取了控制权
{
lock.notify();//继续执行
while (apple > 0)
{
try
{
System.out.println(Thread.currentThread().getName() + ":卖出了第" + (31 - apple) + "个apple");
--apple;
Thread.yield();//給同优先级的线程让步
lock.wait(100);//释放对象锁,让出lock对象,让别的线程执行
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
}
class TR implements Runnable {
private int iphone = 30;
private Object obj = "";
public int sell()
{
//如果此处为while循环将只有一个线程执行,另一个线程无法执行
if (iphone > 0)
{
System.out.println(Thread.currentThread().getName() + ":卖出了第" + (31 - iphone) + "个iphone");
--iphone;
}
return 30 - iphone;
}
@Override
public void run()
{
try
{
while (iphone > 0)
{
synchronized (obj)//获得控制权
{
obj.notify();//继续执行
sell();
obj.wait(100);//释放控制权
Thread.sleep(100);//睡觉100ms.醒来接着买,但是不释放控制权
}
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
执行结果如下:
pool-1-thread-1:卖出了第1个apple
pool-1-thread-2:卖出了第2个apple
pool-1-thread-1:卖出了第3个apple
pool-1-thread-4:卖出了第1个iphone
pool-1-thread-3:卖出了第4个apple
pool-1-thread-2:卖出了第5个apple
pool-1-thread-5:卖出了第2个iphone
pool-1-thread-1:卖出了第6个apple
pool-1-thread-2:卖出了第7个apple
pool-1-thread-3:卖出了第8个apple
pool-1-thread-4:卖出了第3个iphone
pool-1-thread-5:卖出了第4个iphone
pool-1-thread-1:卖出了第9个apple
pool-1-thread-3:卖出了第10个apple
pool-1-thread-2:卖出了第11个apple
pool-1-thread-2:卖出了第12个apple
pool-1-thread-1:卖出了第13个apple
pool-1-thread-3:卖出了第14个apple
pool-1-thread-3:卖出了第15个apple
pool-1-thread-5:卖出了第5个iphone
pool-1-thread-2:卖出了第16个apple
pool-1-thread-4:卖出了第6个iphone
pool-1-thread-1:卖出了第17个apple
pool-1-thread-1:卖出了第18个apple
pool-1-thread-5:卖出了第7个iphone
pool-1-thread-3:卖出了第19个apple
pool-1-thread-2:卖出了第20个apple
pool-1-thread-2:卖出了第21个apple
pool-1-thread-4:卖出了第8个iphone
pool-1-thread-1:卖出了第22个apple
pool-1-thread-3:卖出了第23个apple
pool-1-thread-3:卖出了第24个apple
pool-1-thread-5:卖出了第9个iphone
pool-1-thread-2:卖出了第25个apple
pool-1-thread-1:卖出了第26个apple
pool-1-thread-1:卖出了第27个apple
pool-1-thread-4:卖出了第10个iphone
pool-1-thread-3:卖出了第28个apple
pool-1-thread-2:卖出了第29个apple
pool-1-thread-2:卖出了第30个apple
pool-1-thread-5:卖出了第11个iphone
pool-1-thread-4:卖出了第12个iphone
pool-1-thread-5:卖出了第13个iphone
pool-1-thread-4:卖出了第14个iphone
pool-1-thread-5:卖出了第15个iphone
pool-1-thread-4:卖出了第16个iphone
pool-1-thread-5:卖出了第17个iphone
pool-1-thread-4:卖出了第18个iphone
pool-1-thread-5:卖出了第19个iphone
pool-1-thread-5:卖出了第20个iphone
pool-1-thread-4:卖出了第21个iphone
pool-1-thread-4:卖出了第22个iphone
pool-1-thread-5:卖出了第23个iphone
pool-1-thread-4:卖出了第24个iphone
pool-1-thread-5:卖出了第25个iphone
pool-1-thread-4:卖出了第26个iphone
pool-1-thread-4:卖出了第27个iphone
pool-1-thread-5:卖出了第28个iphone
pool-1-thread-5:卖出了第29个iphone
pool-1-thread-4:卖出了第30个iphone
Process finished with exit code 0