问答题
1.volatile关键字有什么作用?
在JVM1.2之前,Java总是从住存读取变量,但随着JVM的优化,线程可以把主存变量保存在寄存器(工作内存)中操作,线程结束再与主存变量进行同步,然而,当线程没有执行结束就发生了互换这就可能造成一个线程在主存中修改了一个变量的值,而另外一个线程还继续使用它在寄存器中变量的值,而另外一个线程还在继续使用它在寄存器中变量值的副本,造成数据的不一致。要解决这个问题,就需要把把该变量声明为volatile(不稳定的),它指示JVM这个变量是不稳定的,每次使用它都存到主存中进行读取,因此多线程环境下volatile关键字的使用变得非常重要。一般来说,多线程环境下各线程间共享的变量都应该加volatile修饰。
2.编写Java程序模拟烧水泡茶最优工序。
class XishuiHu extends Thread{
public void run(){
System.out.println("开始洗水壶");
try {
Thread.sleep(99);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
System.out.println("结束洗水壶");
}
}
class ShaoShui extends Thread{
public void run(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("开始烧水");
try {
Thread.sleep(1600);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
System.out.println("结束烧水");
}
}
class XiChaHu extends Thread{
public void run(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("开始洗茶壶");
try {
Thread.sleep(200);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
System.out.println("结束洗茶壶");
}
}
class XiChaBei extends Thread{
public void run(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("开始洗茶杯");
try {
Thread.sleep(400);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
System.out.println("结束洗茶杯");
}
}
class NaChaYe extends Thread{
public void run(){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("开始拿茶叶");
try {
Thread.sleep(500);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
System.out.println("结束拿茶叶");
}
}
class PaoCha extends Thread{
public void run(){
try {
Thread.sleep(1601);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("泡茶,请品尝!");
}
}
public class PaoChaYouHua {
public static void main(String args[]){
long start, end, startb, endb;
Thread a = new XishuiHu();
Thread b = new ShaoShui();
Thread c = new XiChaHu();
Thread d = new XiChaBei();
Thread e = new NaChaYe();
Thread f = new PaoCha();
a.start();
try {
// a.join(100);
Thread.sleep(99);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
b.start();
try {
// b.join(200);
Thread.sleep(200);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
c.start();
try {
c.join(400);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
d.start();
try {
d.join(500);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
e.start();
try {
e.join(600);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
f.start();
}
}
3.编写一个基于多线程的生产者/消费者Java应用,各产生10个生产者和消费者线程,共享一个缓冲区队列(长度自设),生产者线程将产品放入到缓冲区,消费者线程从缓冲区取出产品。
class Account
{
volatile private int value;
synchronized void put(int i)
{
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
value = value + i;
System.out.println(Thread.currentThread().getName()+" 生产者--存入了 "+i+" 余量为:"+value);
}
synchronized int get(int i)
{
try {
Thread.sleep(300);
} catch (InterruptedException e) {
}
if (value>i)
value = value - i;
else
{ i = value;
value = 0;
}
System.out.println(Thread.currentThread().getName()+ " 消费者--消费了 "+i+" 余量为:"+value);
return i;
}
}
class Save implements Runnable
{
private Account a1;
public Save(Account a1)
{
this.a1 = a1;
}
public void run()
{
while(true){
a1.put(1);
}
}
}
class Fetch implements Runnable
{
private Account a1;
public Fetch(Account a1)
{this.a1 = a1 ;}
public void run()
{
while(true){
a1.get(1);
}
}
}
public class Test{
public static void main(String[] args){
Account a1 = new Account();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
new Thread(new Save(a1)).start();
//10个生产者和消费者
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
new Thread(new Fetch(a1)).start();
}
}