1.volatile关键字有什么作用?
解决可见性问题:禁用CPU缓存,直接在内存中操作,提高安全性。
2.编写Java程序模拟烧水泡茶最优工序。
class XiShuihu implements Runnable{
public void run(){
System.out.println("洗水壶!");
try{
Thread.sleep(200); //休眠
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
class ShaoShui implements Runnable{
public void run(){
for(int i=1;i<16;i++){
System.out.println("烧水的第"+i+"分钟~");
try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class PaoCha implements Runnable{
public void run() {
System.out.println("泡茶!");
try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
System.out.println("泡茶结束!");
}
}
class XiChahu implements Runnable{
public void run(){
System.out.println("洗茶壶!");
try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
class XiChabei implements Runnable{
public void run(){
for(int i=1;i<3;i++){
System.out.println("洗茶杯的第"+i+"分钟~");
try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
}
class NaChaye implements Runnable{
public void run(){
System.out.println("拿茶叶!");
try{
Thread.sleep(200);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
public class ZhuCha{
public static void main(String[]args) {
XiShuihu xiShuihu=new XiShuihu();
ShaoShui shaoShui=new ShaoShui();
PaoCha paoCha=new PaoCha();
XiChahu xiChahu=new XiChahu();
XiChabei xiChabei=new XiChabei();
NaChaye naChaye=new NaChaye();
Thread t1=new Thread (xiShuihu);
Thread t2=new Thread (shaoShui);
Thread t3=new Thread (paoCha);
Thread t4=new Thread (xiChahu);
Thread t5=new Thread (xiChabei);
Thread t6=new Thread (naChaye);
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
t4.start();
try {
t4.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t5.start();
try {
t5.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t6.start();
try {
t6.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
t3.start();
}
}
其中一种输出:
- 编写一个基于多线程的生产者/消费者Java应用,各产生10个生产者和消费者线程,共享一个缓冲区队列(长度自设),生产者线程将产品放入到缓冲区,消费者线程从缓冲区取出产品。(选做)
class Chanpin //物品类
{
volatile private int num;
//布尔标志
volatile private boolean isChanpin = false;
//put设为同步方法
synchronized void put(int i) {
while(isChanpin){
try{ wait();} //线程等待
catch(Exception e){}
}
num = num + i;
System.out.println("生产"+i+"个产品 总产品:"+num);
isChanpin = true;//设置标志
notifyAll(); //唤醒等待资源的所有线程
}
synchronized int get(int i) {//同步方法
while(!isChanpin){
try { wait();}
catch(Exception e){}
}
if (num>i)
num = num - i;
else {
i = num;
num = 0;
}
System.out.println("取走"+i+"个产品 总产品:"+num);
isChanpin = false;
notifyAll();
return i;
}
}
class Shengchanzhe implements Runnable
{
private Chanpin a1;
public Shengchanzhe(Chanpin a1)
{
this.a1 = a1;
}
public void run()
{
while(true){
a1.put(100);
}
}
}
class Xiaofeizhe implements Runnable
{
private Chanpin a1;
public Xiaofeizhe(Chanpin a1)
{this.a1 = a1 ;}
public void run()
{
while(true){
a1.get(100);
}
}
}
public class Commu{
public static void main(String[] args){
Chanpin a1 = new Chanpin();
new Thread(new Shengchanzhe(a1)).start();//1
new Thread(new Shengchanzhe(a1)).start();//2
new Thread(new Shengchanzhe(a1)).start();//3
new Thread(new Shengchanzhe(a1)).start();//4
new Thread(new Shengchanzhe(a1)).start();//5
new Thread(new Shengchanzhe(a1)).start();//6
new Thread(new Shengchanzhe(a1)).start();//7
new Thread(new Shengchanzhe(a1)).start();//8
new Thread(new Shengchanzhe(a1)).start();//9
new Thread(new Shengchanzhe(a1)).start();//10
new Thread(new Xiaofeizhe(a1)).start();//1
new Thread(new Xiaofeizhe(a1)).start();//2
new Thread(new Xiaofeizhe(a1)).start();//3
new Thread(new Xiaofeizhe(a1)).start();//4
new Thread(new Xiaofeizhe(a1)).start();//5
new Thread(new Xiaofeizhe(a1)).start();//6
new Thread(new Xiaofeizhe(a1)).start();//7
new Thread(new Xiaofeizhe(a1)).start();//8
new Thread(new Xiaofeizhe(a1)).start();//9
new Thread(new Xiaofeizhe(a1)).start();//10
}
}