1.FutureTask类有什么作用?它实现了哪些接口?Callable接口和Runnable接口有什么不同?
FutureTask类的作用是用于线程需要返回执行结果时。
它实现了Future接口和Runnable接口。
两个接口的不同之处:Callable可以返回一个类型V,而Runnable却不行;Callable可以抛出checked exception,而Runnable不行;Thread类只支持Runnable接口,不支持Callable接口。
2.volatile关键字有什么作用?
首先,volatile关键字可以保证可见性,但是无法保证原子性;其次是volatile关键字会自动禁止指令的重排;
3.Java提供了哪些同步机制来实现互斥?
Java提供了两种锁的机制来保证多个线程之间实现互斥机制。第一种是synchronized,另一种是ReentrantLock。
4.编写Java程序模拟烧水泡茶最优工序
//烧水
class HeatUpWater implements Runnable{
public void run(){
System.out.println("Begin to heat water");
try{
Thread.sleep(15000);
}catch (Exception e){
e.printStackTrace();
System.out.println("Heat up water is in the Error!");
}
System.out.println("Finish to heat water");
}
}
//洗茶杯
class WashCup implements Runnable{
public void run(){
System.out.println("Begin to wash cup");
try{
Thread.sleep(2000);
}catch (Exception e){
e.printStackTrace();
System.out.println("WashCup is in the Error!");
}
System.out.println("Finish to wash cup");
}
}
//洗水壶
class Washkettle implements Runnable{
public void run(){
System.out.println("Begin to wash Kettle");
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
System.out.println("WashKettle is in the Error!");
}
System.out.println("Finish to wash Kettle");
}
}
//洗茶壶
class WashTeapot implements Runnable{
public void run(){
System.out.println("Begin to wash Teapot");
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
System.out.println("WashTeapot is in the Error!");
}
System.out.println("Finish to wash Teapot");
}
}
//拿茶叶
class GetTea implements Runnable{
public void run(){
System.out.println("Begin to get Tea");
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
System.out.println("get Tea is in the Error!");
}
System.out.println("Finish to get Tea");
}
}
public class soaking{
public static void main(String[] args)throws Exception{
Thread t1=new Thread(new Washkettle());
Thread t2=new Thread(new HeatUpWater());
Thread t3=new Thread(new WashTeapot());
Thread t4=new Thread(new WashCup());
Thread t5=new Thread(new GetTea());
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
t4.start();
t4.join();
t5.start();
t5.join();
System.out.println("Finish soaking!");
}
}
5.请使用Java并发包的Lock及Conditon改写例9.11
class Accounter{
volatile private int value;
//布尔标志
volatile private boolean isMoney = false;
//put设为同步方法
synchronized void put(int i) {
while(isMoney) {
try{
wait(); //等待并释放锁
} //线程等待
catch(Exception e){}
}
value = value + i;
System.out.println("存入"+i+" 账上金额为:"+value);
isMoney = true;//设置标志
notifyAll(); //唤醒等待资源的所有线程
}
synchronized int get(int i) {
while(!isMoney ){
try {
wait();
}
catch(Exception e){}
}
if (value>i)
value = value - i;
else {
i = value;
value = 0;
}
System.out.println("取走"+i+" 账上金额为:"+value);
isMoney = false;
notifyAll(); //并不释放锁
return i;
}
}
class Products{
public static void main(String[] args) {
Accounter a1=new Accounter();
//存钱线程
new Thread(() -> {
while(true){ a1.put(100);}
}).start();
//取钱线程
new Thread(() -> {
while(true){ a1.get(100); }
}).start();
}
}
import java.util.concurrent.locks.*;
class Accounter{
volatile private int value;
//布尔标志
volatile private boolean isMoney = false;
private final ReentrantLock lock=new ReentrantLock();
private Condition SaveCondition=lock.newCondition();
private Condition FetchCondition=lock.newCondition();
//put设为同步方法
void put(int i) {
lock.lock();
try {
while (isMoney) {
try {
SaveCondition.await();
} catch (Exception e) {}
}
value = value + i;
System.out.println("存入" + i + " 账上金额为:" + value);
isMoney = true;//设置标志
FetchCondition.signal();
}finally{
lock.unlock();
}
}
int get(int i) {
lock.lock();
try {
while (!isMoney) {
try {
FetchCondition.await();
} catch (Exception e) {
}
}
if (value > i)
value = value - i;
else {
i = value;
value = 0;
}
System.out.println("取走" + i + " 账上金额为:" + value);
isMoney = false;
SaveCondition.signal();
return i;
}finally{
lock.unlock();
}
}
}
class Products{
public static void main(String[] args) {
Accounter a1=new Accounter();
//存钱线程
new Thread(() -> {
while(true){ a1.put(100);}
}).start();
//取钱线程
new Thread(() -> {
while(true){ a1.get(100); }
}).start();
}
}
6.编写一个多线程Java应用模拟生产者/消费者模型,各产生10个生产者和消费者线程,共享一个缓冲区队列(长度自设),生产者线程将产品放入到缓冲区,消费者线程从缓冲区取出产品。
mport java.util.*;
class Products {
volatile private int product=0;
public Integer flag ;
Products(){
flag=new Integer(1);
}
public int getProduct() {
return product;
}
public void produceProduct(int a) {
synchronized(flag) {
if(product<100) {
if(product+a>100) {
int b=a;
a=100-product;
product=100;
System.out.println(Thread.currentThread().getName()+"produce "+b+", produce too many only accept"+a+"; Products:"+product);
}else {
product+=a;
System.out.println(Thread.currentThread().getName()+" produce "+a+"; Products:"+product);
}
}
}
}
public void consumeProduct(int a) {
synchronized(flag) {
if(product>0) {
if(product-a<0) {
int b=product;
product=0;
System.out.println(Thread.currentThread().getName()+"consume "+a+", consume too maney only supply"+b+"; Products:"+product);
}else {
product-=a;
System.out.println(Thread.currentThread().getName()+" consume "+a+"; Products:"+product);
}
}
}
}
public static void main(String[] args) {
Products pr=new Products();
Producer[] p;
p=new Producer[10];
for(int i=0;i<10;i++) {
p[i]=new Producer(pr);
}
Consumer[] c;
c=new Consumer[10];
for(int i=0;i<10;i++) {
c[i]=new Consumer(pr);
}
for(int i=0;i<10;i++) {
p[i].start();
}
for(int i=0;i<10;i++) {
c[i].start();
}
}
}
class Producer extends Thread{
private Products p;
private Random ra;
Producer(Products p){
this.p=p;
ra=new Random();
}
public void run() {
int a;
while(true) {
a=ra.nextInt(Integer.MAX_VALUE)%10;
p.produceProduct(a);
}
}
}
class Consumer extends Thread{
private Products p;
private Random ra;
Consumer(Products p){
this.p=p;
ra=new Random();
}
public void run() {
int a;
while(true) {
a=ra.nextInt(Integer.MAX_VALUE)%10;
p.consumeProduct(a);
}
}
}