注意注意注意!Lambda是1.8的新特性,之前的版本用不了的。lambda主要用于简化代码,对于提高效率,减少内存等没有作用
线程间通信
最典型的例题就是生产者与消费者模型:
1)定义一个生产手机车间的类PhoneFactory
2)定义一个生产线程任务的类ProduceTask 并实现线程任务接口Runnable,同时实现接口中的run函数;
3)定义一个消费线程任务的类ConsumeTask 并实现线程任务接口Runnable,同时实现接口中的run函数;
4)定义测试类
public class PhoneFactory {
private String name;//手机名
private double price;//手机价格
int i = 0;
boolean hasPhone=false;//判断有没有手机
//生产
public synchronized void produce(){
if(hasPhone){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(i%2==0){
this.name="IPhone7Plus";
this.price=6888.00;
}else{
this.name="华为";
this.price=1000.00;
}
this.hasPhone=true;//有手机了,修改标记
this.notify();//唤醒
i++;
}
//消费
public synchronized void get(){
if(!hasPhone){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//public void get(){
System.out.println("消费的手机名为:"+name+",价格为:"+price);
this.hasPhone=false;//没手机了
this.notify();//唤醒生产者来生产
}
}
public class ProduceTask implements Runnable {
PhoneFactory pf;
public ProduceTask(PhoneFactory pf) {
this.pf=pf;
}
@Override
public void run() {
while (true){
try { Thread.sleep(10);} catch (InterruptedException e) { e.printStackTrace(); }
pf.produce();
}
}
}
public class ConsumeTask implements Runnable {
PhoneFactory pf;
public ConsumeTask(PhoneFactory pf) {
this.pf=pf;
}
@Override
public void run() {
while (true){
try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
pf.get();
}
}
}
public class Test {
public static void main(String[] args) {
PhoneFactory pf = new PhoneFactory();
ProduceTask pt = new ProduceTask(pf);
ConsumeTask ct = new ConsumeTask(pf);
Thread t1 = new Thread(pt,"生产者");
Thread t2 = new Thread(ct,"消费者");
t1.start();
t2.start();
}
}
线程间通信的方式:
A:wait()让线程等待;
B:notify()唤醒某个等待中的线程;
C:notifyAll()唤醒所有等待的线程;
注意:为什么这几个方法都在Object类中定义呢?
线程的通信是由锁对象控制的,锁对象可以是任意的。任意对象都可以调用的方法必须在Object类中
线程池
合理利用线程池能够带来三个好处:
- 降低资源消耗。
- 提高响应速度。
- 提高线程的可管理性
线程池的使用就用上面那个Test类来举例说明一下吧!
public class Test {
public static void main(String[] args) {
PhoneFactory pf = new PhoneFactory();
ProduceTask pt = new ProduceTask(pf);
ConsumeTask ct = new ConsumeTask(pf);
/*Thread t1 = new Thread(pt,"生产者");
Thread t2 = new Thread(ct,"消费者");
t1.start();
t2.start();*/
ExecutorService es = Executors.newFixedThreadPool(2);
es.excute(pt);
es.excute(ct);
}
}
Lambda表达式(1.8特性)
1.Lambda(无参无返回)
//导演接口类
public interface Director {
void makeMovie();
}
public class Test09InvokeDirect {
public static void main(String[] args) {
invokeDirect(()-> System.out.println("导演拍电影啦!"));
}
private static void invokeDirect(Director director) {
director.makeMovie();
}
}
2.Lambda(有参有返回)
public interface Calculator {
int calc(int a, int b);
}
public class Test {
public static void main(String[] args) {
invokeCalculator(10,20,(a,b)->a-b);
}
private static void invokeCalculator(int a, int b, Calculator c) {
int result=c.calc(a, b);
System.out.println(result);
}
}
3.Lambda(省略格式)
public class Student {
private String name;
private int grade;
//set、get、构造方法、toString方法省略
}
public class Test {
public static void main(String[] args) {
Student[] arr={
new Student("label",70),
new Student("elam",90),
new Student("yumi",60),
};
Arrays.sort(arr,(s1,s2)->s2.getGrade()-s1.getGrade());
System.out.println(Arrays.toString(arr));
}
}