1.FutureTask类有什么作用?它实现了哪些接口?Callable接口和Runnable接口有什么不同?
FutureTask实现了Runnable和Future接口
- Callable可以返回一个类型V,而Runnable不可以
- Callable能够抛出checked exception,而Runnable不可以。
- Runnable是自从java1.1就有了,而Callable是1.5之后才加上去的
- Callable和Runnable都可以应用于executors。而Thread类只支持Runnable.
2.请查阅JDK自学线程池的相关类,如ThreadPoolExecutor构造器各个参数的意义, 利用线程池编写多线程程序。
3.volatile关键字有什么作用?
1、保证该变量对所有线程的可见性;
2、禁止指令重排序优化。
4.Java提供了哪些同步机制来实现互斥?
1. 对象监视器(monitor)与synchronized
Synchronized的三种用法:
synchronized 代码块:监视器就是指定的对象。
synchronized 方法:监视器就是this对象。
synchronized 静态方法:监视器就是相应的Class对象
2. Java并发包中的锁类:Lock接口,ReentrantLock类
5.编写Java程序模拟烧水泡茶最优工序。
package threaddddd;
class xiShuiHu extends Thread{
private Integer i;
xiShuiHu(Integer ii){
this.i=ii;
}
public synchronized void run() {
int ww=0;
while(++ww<=1) {
++i;
int w=0;
while(++w<=1000) ;
System.out.println("xiShuiHu--"+i);
}
}
}
class shaoShui implements Runnable{
private Integer i;
public int ww=0;
shaoShui(Integer ii){
this.i=ii;
}
public void run() {
while(++ww<=15) {
++i;
int w=0;
while(++w<=1000) ;
System.out.println("shaoShui--"+i);
}
}
}
class xiXiNa implements Runnable{//包含了洗茶壶,洗茶杯,拿茶叶
private Integer i;
public int ww=0;
xiXiNa(Integer ii){
this.i=ii;
}
public void run() {
while(++ww<=4) {
++i;
int w=0;
while(++w<=1000) ;
System.out.println("xiXiNa--"+i);
}
}
}
class paoCha implements Runnable{
private Integer i;
paoCha(Integer ii){
this.i=ii;
}
public void run() {
int m=0;
while(++m<=10) {
++i;
int w=0;
while(++w<=1000) ;
System.out.println("paoCha--"+i);
}
}
}
public class zhu {
public static void main(String[] args) {
Thread xsh =new xiShuiHu(0);
xsh.start();
try {
xsh.join();}
catch(InterruptedException it){}
shaoShui r=new shaoShui(0);
Thread sh=new Thread(r);
xiXiNa r1=new xiXiNa(0);
Thread xxn=new Thread(r1);
sh.start();
xxn.start();
try {
sh.join();}
catch(InterruptedException it){}
try {
xxn.join();}
catch(InterruptedException it){}
paoCha r2=new paoCha(0);
Thread pc=new Thread(r2);
pc.start();
}
}
6.请使用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();
}
}
7. 编写一个多线程Java应用模拟生产者/消费者模型,各产生10个生产者和消费者线程,共享一个缓冲区队列(长度自设),生产者线程将产品放入到缓冲区,消费者线程从缓冲区取出产品。
package day1101;
import java.util.LinkedList;
/**
* 生产者消费者问题
*/
public class ProAndCon {
//最大容量
public static final int MAX_SIZE = 2;
//存储媒介
public static LinkedList<Integer> list = new LinkedList<>();
class Producer implements Runnable {
@Override
public void run() {
synchronized (list) {
//仓库容量已经达到最大值
while (list.size() == MAX_SIZE) {
System.out.println("仓库已满,生产者" + Thread.currentThread().getName() + "不可生产.");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(1);
System.out.println("生产者" + Thread.currentThread().getName() + "生产, 仓库容量为" + list.size());
list.notify();
}
}
}
class Consumer implements Runnable {
@Override
public void run() {
synchronized (list) {
while (list.size() == 0) {
System.out.println("仓库为空,消费者" + Thread.currentThread().getName() + "不可消费.");
try {
list.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.removeFirst();
System.out.println("消费者" + Thread.currentThread().getName() + "消费,仓库容量为" + list.size());
list.notify();
}
}
}
public static void main(String[] args) {
ProAndCon proAndCon = new ProAndCon();
Producer producer = proAndCon.new Producer();
Consumer consumer = proAndCon.new Consumer();
for (int i = 0; i < 10; i++) {
Thread pro = new Thread(producer);
pro.start();
Thread con = new Thread(consumer);
con.start();
}
}
}