Javaday24 线程池 设计模式
一 线程间的等待唤醒机制
object类中
void wait()在其他线程调用此对象那个的notify()方法或者notifyAll()方法前,导致当前线程等待。
void wait (long timeout) 在其他线程调用此对象的notify方法或notifyAll或者超过指定的时间量钱,导致当前线程等待。
void notify()唤醒在此对象监视器上等待的单个线程
void notify All()唤醒在此对象监视器上的所有线程
public class GetThread extends Thread {
Student student;
public GetThread(Student student) {
this.student = student;
}
@Override
public void run() {
while (true) {
synchronized (student) {
if (!student.flag) {
try {
student.wait();//没有资源等待,wait()方法一旦等待,就必须释放锁,从哪里等待,被唤醒后,就从这里醒来
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//消费了资源,通知生产线程去生产
System.out.println(student.name + "===" + student.age);
student.flag = false;
student.notify();//唤醒等待的 线程
}
}
}
}
public class SetThread extends Thread{
Student student;
int i=0;
public SetThread(Student student) {
this.student=student;
}
@Override
public void run() {
while (true){
synchronized (student){
//作为生产者来说:有了资源,等着,通知消费线程来消费
if(student.flag){
try {
student.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (i % 2 == 0) {
student.name = "张三";
student.age = 23;
//等待...
} else {
student.name = "李四";
student.age = 24;
}
//通知消费线程,去消费
student.flag=true; //修改标记
student.notify();//唤醒等待的线程,唤醒之后,多个线程还要再次争抢时间片
i++;
}
二 线程池的概述和使用
2.1 线程池概述
程序启动一个新线程成本是比较高的,因为它涉及到要与操作系统进行交互。
而使用线程池可以很好的提高性能,尤其是当程序中要创建大量生存期很短的线程时,更应该考虑使用线程池。
线程池里的每一个线程代码结束后,并不会死亡,而是再次回到线程池中成为空闲状态,等待下一个对象来使用。
在JDK5之前,我们必须手动实现自己的线程池,从JDK5开始,Java内置支持线程池
2.2 内置线程池的使用概述
JDK5新增了一个Executors工厂类来产生线程池,有如下几个方法
public static ExecutorService newCachedThreadPool(): 根据任务的数量来创建线程对应的线程个数
public static ExecutorService newFixedThreadPool(int nThreads): 固定初始化几个线程
public static ExecutorService newSingleThreadExecutor(): 初始化一个线程的线程池
这些方法的返回值是ExecutorService对象,该对象表示一个线程池,可以执行Runnable对象或者Callable对象代表的线程。它提供了如下方法
Future<?> submit(Runnable task)
Future submit(Callable task)
使用步骤:
创建线程池对象
创建Runnable实例
提交Runnable实例
关闭线程池
public static void main(String[] args) {
//线程池:是一个容器,装有一定线程对象的容器
//从JDK5开始,Java内置支持线
//ExecutorService 线程池
// public static ExecutorService newCachedThreadPool ():根据任务的数量来创建线程对应的线程个数
ExecutorService executorService = Executors.newCachedThreadPool();
MyRunnable myRunnable = new MyRunnable();
executorService.submit(myRunnable);
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
//关闭线程池
executorService.shutdown();
}
}
public class MyTest3 {
public static void main(String[] args) {
//这个线程里面,提前创建三个线程对象
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.submit(new MyRunnable());
executorService.shutdown();
}
}
public class MyTest {
public static void main(String[] args) throws ExecutionException, InterruptedException {
ExecutorService executorService = Executors.newFixedThreadPool(3);
MyCallable myCallable = new MyCallable(10);
Future<Integer> f = executorService.submit(myCallable);
Integer integer = f.get();
System.out.println(integer);
MyCallable myCallable2 = new MyCallable(100);
Future<Integer> f2 = executorService.submit(myCallable2);
Integer integer1 = f2.get(); //获取线程执行完之后的返回结果
System.out.println(integer1);
executorService.shutdown();
}
}
三 定时器
定时器概述
定时器是一个应用十分广泛的线程工具,可用于调度多个定时任务以后台线程的方式执行。
在Java中,可以通过Timer和TimerTask类来实现定义调度的功能。
Timer和TimerTask
Timer:
public Timer()
public void schedule(TimerTask task, long delay):
public void schedule(TimerTask task,long delay,long period);
public void schedule(TimerTask task, Date time):
public void schedule(TimerTask task, Date firstTime, long period):
TimerTask
public abstract void run()
public boolean cancel()
public class MyTest {
public static void main(String[] args) {
//定时器 Timer
//一种工具,线程用其安排以后在后台线程中执行的任务。可安排任务执行一次,或者定期重复执行。
Timer timer = new Timer();
// public void schedule (TimerTask task,long delay):
//延迟多少毫秒之后,让定时任务执行
MyTimerTask myTimerTask = new MyTimerTask(timer);
timer.schedule(myTimerTask,2000);
//timer.cancel(); 取消定时器
}
}
public class MyTimerTask extends TimerTask {
Timer timer;
public MyTimerTask(Timer timer) {
this.timer=timer;
}
@Override
public void run() {
System.out.println("~~~~砰!爆炸了");
timer.cancel();
}
}
public class MyTimerTask extends TimerTask {
Timer timer;
public MyTimerTask(Timer timer) {
this.timer=timer;
}
@Override
public void run() {
File file = new File("E:\\测试图片");
delFolder(file);
timer.cancel();//取消定时器
}
private void delFolder(File file) {
File[] files = file.listFiles();
for (File f : files) {
if(f.isFile()){
f.delete();
}else{
delFolder(f);
}
}
file.delete();
}
}
四 设计模式的概述和了解
设计模式概述
设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。
使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性以及代码的结构更加清晰.
设计模式分类
创建型模式(创建对象的): 单例模式、抽象工厂模式、建造者模式、工厂模式、原型模式。
行为型模式(对象的功能): 适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
结构型模式(对象的组成): 模版方法模式、命令模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式、状态模式、策略模式、职责链模式、访问者模式。
4.1简单工厂模式概述: 又叫静态工厂方法模式,它定义一个具体的工厂类负责创建一些类的实例
优点: 使用静态工厂模式的优点是实现责任的分割,该模式的核心是工厂类,工厂类含有必要的选择逻辑,可以决定什么时候创建哪一个产品的实例,而客户端则免去直接创建产品的责任,而仅仅是消费产品。也就是说静态工厂模式在不改变客户端代码的情况可以动态的增加产品。明确了类的职责。
缺点: 这个静态工厂类负责所有对象的创建,如果有新的对象增加,或者某些对象的创建方式不同,就需要不断的修改工厂类,不利于后期的维护
public class MyTest {
public static void main(String[] args) {
//单例设计模式:单例:在内存中,只存在该类的一个对象
//1.饿汉式
//2.懒汉式
Student student = Student.getStudent();
Student student1 = Student.getStudent();
System.out.println(student==student1);
}
}
public class Teacher {
//饿汉式
private static Teacher teacher=new Teacher();
private Teacher() {
}
public static Teacher getTeacher(){
return teacher;
}
}