QUESTIONS:
1.Future Task作用,实现接口:作用:
FutureTask 可用于异步获取执行结果或取消执行任务。经过传入Runnable 或者 Callable 的任务给 FutureTask ,直接调用其run方法或者放入线程池执行,以后能够在外部经过FutureTask的get方法异步获取执行结果,。主线程能够在完成本身的任务后,再去获取结果。另外,FutureTask还能够确保即便调用了屡次run方法,它都只会执行一次Runnable或者Callable任务等。
接口:
实现了Futrue 和 Runnable(FutrueTask是Futrue接口的唯一的实现类)
不同点:
1.Runnable没有返回值,而Callable可以返回执行结果
2.Callable接口的call()方法允许抛出异常,而Runnable的run()方法不能抛出异常。异常只能在内部消化,不能往上继续抛
这里选取了最后一个构造方法,包含了所有的参数。
- corePoolSize (int) : 为核心线程池大小
- maximumPoolSize (int) : 为最大线程池大小
- keepAliveTime (long) : 为线程最大空闲时间
- unit (TimeUnit) : 为时间单位
- workQueue (BlockingQueue) : 为线程等待队列
- threadFactory (ThreadFactory) : 为线程创建工坊
- handler (RejectedExecutionHandler) : 为拒绝策略
public static void main(String[] args) {
ExecutorService service = new ThreadPoolExecutor(5, 10, 300, TimeUnit.MILLISECONDS,
new ArrayBlockingQueue(5));
Runnable run = () -> {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + "正在执行");
} catch (InterruptedException e) {
e.printStackTrace();
}
};
for (int i = 0; i < 10; i++) {
service.execute(run);
}
service.shutdown();
}
3.volatile关键字有什么作用:
在JVM 1.2 前,java总是从主存读取变量,但随着JVM优化,线程可以把主存变量保存在寄存器中操作,线程结束后再与主存变量同步。然而,当线程没有执行结束就发生了互换,这就可能导致一个线程在主存中修改了一个变量的值,而另外一个线程还在继续使用它在寄存器中变量值的副本,造成数据的不一致。要解决这个问题,需要把变量声明为volatile(不稳定的),它指示JVM这个变量是不稳定的,每次使用它都需要读取。一般来说,多线程环境下各线程间共享变量都应该加volatile修饰。
4.java提供了哪些同步机制实现互斥:java通过引入synchronized实现互斥。线程互斥可以看成是一种特殊的线程同步。
Synchronized有如下三种用法:
1)synchronized代码块:监视器为指定对象
2)synchronized方法:监视器为this对象
3)synchronized静态方法:监视器为相应的类
当使用synchronized锁住多段不同代码片段,但同步块使用的同步监视器对象为同一个时,这些代码片段互斥,多个线程不能同时执行它们。
5.编写Java模拟烧水泡茶最优工序:package lianxia;
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 threaddddd {
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.用lock,condition改写9.11:
package lianxi;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Account{
volatile private int value;
volatile private boolean isMoney = false;
public ReentrantLock lock = new ReentrantLock();
public Condition condition = lock.newCondition();
void put(int i) {
try {
lock.lock();
if(isMoney) {
try {
condition.await();
}
catch(Exception e) {}
}
value = value+i;
System.out.println("存入"+i+"账上金额为:"+value);
isMoney = true;
}finally {
lock.unlock();
}
}
int get(int i) {
try {
lock.lock();
if(!isMoney) {
try {condition.await();}
catch(Exception e) {}
}
if(value>i) {
value = value-i;
}
else {
i=value;
value=0;
}
System.out.println("取走"+i+"账上金额为:"+value);
isMoney = false;
return i;
}catch(Exception e) {}
}
}
7.编写多线程模拟生产者消费者模型:
package lianxi;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.linkedBlockingQueue;
public class BlockingQueueTest2 {
public class Queue {
BlockingQueue queue = new linkedBlockingQueue(5);
//生产
public void produce() throws InterruptedException {
queue.put("A production");
}
//消费
public String consume() throws InterruptedException {
return queue.take();
}
}
//生产者
class Producer implements Runnable {
private String instance;
private Queue queue;
public Producer(String instance, Queue queue) {
this.instance = instance;
this.queue = queue;
}
public void run() {
try {
while (true) {
System.out.println("生产者准备生产:" + instance);
queue.produce();
System.out.println("生产者生产完毕:" + instance);
// 休眠
Thread.sleep(300);
}
} catch (InterruptedException ex) {
System.out.println("Producer Interrupted");
}
}
}
// 消费者
class Consumer implements Runnable {
private String instance;
private Queue queue;
public Consumer(String instance, Queue queue) {
this.instance = instance;
this.queue = queue;
}
public void run() {
try {
while (true) {
// 消费苹果
System.out.println("消费者准备消费:" + instance);
System.out.println(queue.consume());
System.out.println("消费者消费完毕:" + instance);
// 休眠
Thread.sleep(1000);
}
} catch (InterruptedException ex) {
System.out.println("Consumer Interrupted");
}
}
}
public static void main(String[] args) {
BlockingQueueTest2 test = new BlockingQueueTest2();
// 建立队列
Queue queue = test.new Queue();
ExecutorService service = Executors.newCachedThreadPool();
Producer producer = test.new Producer("生产者001", queue);
Producer producer2 = test.new Producer("生产者002", queue);
Producer producer3 = test.new Producer("生产者003", queue);
Producer producer4 = test.new Producer("生产者004", queue);
Producer producer5 = test.new Producer("生产者005", queue);
Producer producer6 = test.new Producer("生产者006", queue);
Producer producer7 = test.new Producer("生产者007", queue);
Producer producer8 = test.new Producer("生产者008", queue);
Producer producer9 = test.new Producer("生产者009", queue);
Producer producer10 = test.new Producer("生产者010", queue);
Consumer consumer = test.new Consumer("消费者001", queue);
Consumer consumer2 = test.new Consumer("消费者002", queue);
Consumer consumer3 = test.new Consumer("消费者003", queue);
Consumer consumer4 = test.new Consumer("消费者004", queue);
Consumer consumer5 = test.new Consumer("消费者005", queue);
Consumer consumer6 = test.new Consumer("消费者006", queue);
Consumer consumer7 = test.new Consumer("消费者007", queue);
Consumer consumer8 = test.new Consumer("消费者008", queue);
Consumer consumer9 = test.new Consumer("消费者009", queue);
Consumer consumer10 = test.new Consumer("消费者010", queue);
service.submit(producer);
service.submit(producer2);
service.submit(producer3);
service.submit(producer4);
service.submit(producer5);
service.submit(producer6);
service.submit(producer7);
service.submit(producer8);
service.submit(producer9);
service.submit(producer10);
service.submit(consumer);
service.submit(consumer2);
service.submit(consumer3);
service.submit(consumer4);
service.submit(consumer5);
service.submit(consumer6);
service.submit(consumer7);
service.submit(consumer8);
service.submit(consumer9);
service.submit(consumer10);
}
}



