一、线程的常用方法
二、Object提供的操作线程的方法
三、多线程生产者与消费者
四、多线程的第三种创建方式
包子类,作为锁对象
package com.test.demo3;
public class BaoZi {
public static String pi;
public static String xian;
static boolean flag = false;
}
代码-消费者
package com.test.demo3;
public class Mymaker implements Runnable {
BaoZi baoZi;
public Mymaker(BaoZi baoZi){
this.baoZi=baoZi;
}
@Override
public void run() {
synchronized (baoZi){
while (true){
if (baoZi.flag){
try {
baoZi.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
baoZi.pi="黄色";
baoZi.xian="玉米";
System.out.println("生产了一个"+baoZi.pi+"\t"+baoZi.xian+"的包子");
baoZi.flag=true;
baoZi.notify();
}
}
}
}
代码-生成者
package com.test.demo3;
public class Myeater extends Thread{
BaoZi baoZi;
public Myeater(BaoZi baoZi){
this.baoZi=baoZi;
}
@Override
public void run() {
while (true){
synchronized (baoZi){
if (!baoZi.flag){
try {
baoZi.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("吃了一个"+baoZi.pi+"\t"+baoZi.xian+"的包子");
baoZi.pi=null;
baoZi.xian=null;
baoZi.flag=false;
baoZi.notify();
}
}
}
}
代码-测试类
package com.test.demo3;
public class Test {
public static void main(String[] args) {
BaoZi baoZi=new BaoZi();
Mymaker maker=new Mymaker(baoZi);
Thread th=new Thread(maker);
Myeater myeater=new Myeater(baoZi);
th.start();
myeater.start();
}
}
五、枚举
1.简介
1.枚举就是用于表示一些固定的值(常量) 可以用枚举来表示,枚举项是对象
2.定义枚举类:
访问修饰符 enum 枚举类的名称(){
枚举项
}
3.特点:
每个枚举类都是Enum的子类
每个枚举项都是一个对象
枚举中可以定义成员变量
枚举类只能有私有的构造方法,默认的就是私有的构造
枚举类中可以有抽象的方法,但是每一个枚举项都必须重写这个抽象方法
枚举项通过枚举类.枚举项来获取,枚举项写在枚举类的第一行,通过逗号分割
2.枚举的方法
枚举经常和switch 结合
package com.qf.stu.demo5;
public class Test {
public static void main(String[] args) {
switch (Week.星期二){
case 星期一:
System.out.println(1);
break;
case 星期二:
System.out.println(2);
break;
case 星期三:
System.out.println(3);
break;
case 星期四:
System.out.println(4);
break;
case 星期五:
System.out.println(5);
break;
case 星期六:
System.out.println(6);
break;
case 星期日:
System.out.println(7);
break;
}
}
}
六、线程的生命周期
1.简介
线程的生命周期状态都是通过枚举项来表示
获取状态:Thread.State
方法的名称
public State getState(){} 获取当前线程的状态
七、线程池
1.简介
常量池:用于存储字符串的常量
线程池:用来管理与维护线程
jdk提供的线程池
2.创建线程池对象的方法
一个是创建只有一个线程的线程池
一个是创建一个可以重用固定线程数的线程池
3.步骤
1.通过线程工具类来创建线程池对象
2.创建一个线程任务对象
3.将对象提交到线程池中 submit(线程任务对象)
通过get()方法获取到线程执行完的结果
4.将任务对象提交到线程池方法
package com.qf.stu.demo14;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class Test1 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
MyCallable my=new MyCallable();
FutureTask<String> ft=new FutureTask<>(my);
ExecutorService executorService1 = Executors.newSingleThreadExecutor();
ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService1.submit(ft);
String s = ft.get();
System.out.println(s);
executorService1.shutdown();
}
}
八、单例设计模式
1.单例设计模式的特点:有且仅实例化一个对象
2.设计场景:加载配置信息 工具类 Spring 容器对象的注入
3.有且仅实例化一个对象:
私有的属性
私有的构造方法
公有的方法
4.单例设计模式的分类:饿汉模式、懒汉模式、双重锁、内部类、枚举
九、定时器
使用步骤
1.创建一个任务对象
2.实例化定时对象
3.将任务提交到定时器中
常用的方法