day23、线程
1、进程
进程是独立运行的应用程序,需要系统进行分配
1、独立性 各个进程之间是相互独立的,互相不影响
2、互斥性 每个进程要分配一个独立的端口号
2、线程
特性: 1、抢占式运行 2、资源共享
并行:真正的同时执行
并发:同时发生,轮流交替执行
3、创建线程
1、继承Thread
继承 Thread,重写run方法,然后分配并启动子类的实例
class AAA extends Thread {
@Override
public void run(){}
}
AAA aaa = new AAA();
aaa.start();
2、实现Runable接口
实现Runnable接口,重写run方法,分配类的实例。创建Thread时,作为参数传递,并启动
class BBB implements Runnable {
@Override
public void run(){}
}
BBB bbb = new BBB();
Thread thread = new Thread(BBB);
thread.start();
3、实现Callable接口
创建MyCallable类,创建MyCallable类的对象,(表示多线程要执行的任务)
创建MyCallable类的对象,(表示多线程要执行的任务)
创建FutureTask的对象,(作用管理多线程运行的结果)
创建Thread类的对象,并启动(表示线程)
class MyCallable implements Callable <返回值的泛型> {
@Override
public 返回值的类型 call() throws Exception {
return null;
}
}
MyCallable mc = new MyCallable();
FutureTask<Integer> ft = new FutureTask<>(mc);
new Thread(ft).start();
Integer result = ft.get();
4、线程方法
构造方法
Thread()
Thread(Runnable target)
Thread(Runnable target, String name)
方法:
static Thread currentThread() 返回对当前执行的线程对象的引用
String getName() 返回此线程的名称
void setName(String name) 更改线程名字
int getPriority() 返回此线程的优先级
void setPriority() 更改此线程的优先级
static void sleep()
5、线程同步和锁
同步方法
public synchronized void eat(){
}
只能有一个进程进入到方法中,其他线程在方法外面等
同步代码块:
synchronized (this) {
}
day24、死锁和等待唤醒
死锁:
class DeadLock implements Runnable {
private boolean flag;//标记
private Object obj1;//对象1
private Object obj2;//对象2
public DeadLock(boolean flag, Object obj1, Object obj2) {
this.flag = flag;
this.obj1 = obj1;
this.obj2 = obj2;
}
@Override
public void run() {
if (flag) {//如果flag = true 让线程1执行这个if语句里面的代码
synchronized (obj1) {
System.out.println(Thread.currentThread().getName() + "拿到了obj1资源");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程1执行了");
synchronized (obj2) {//想用obj2这个资源
System.out.println(Thread.currentThread().getName() + "拿到obj2资源");
}
}
}
if (!flag) {//如果flag=false 线程2 执行这个if语句里面的代码
synchronized (obj2) {
System.out.println(Thread.currentThread().getName() + "拿到了obj2资源");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程2执行了");
synchronized (obj1) {
System.out.println(Thread.currentThread().getName() + "拿到obj1资源");
}
}
}
}
}
public class Demo1 {
public static void main(String[] args) {
Object obj1 = new Object();
Object obj2 = new Object();
DeadLock deadLock1 = new DeadLock(true, obj1, obj2);
new Thread(deadLock1, "线程1").start();
DeadLock deadLock2 = new DeadLock(false, obj1, obj2);
new Thread(deadLock2, "线程1").start();
}
}
等待和唤醒
1、Object 类的wati(), notify(), notifyAll()
2、阻塞队列
实现了4个接口:
Iterable、Collection、Queue、BlockingQueue
有两个实现类: ArrayBlockingQueue,底层是数组,有界
LinkedBlockingQueue,底层是链表,无界
生产者和消费者必须使用同一个阻塞队列
class Cook extends Thread {
ArrayBlockingQueue<String> queue;
public Cook(ArrayBlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run(){
queue.put
}
}
class Foodie extends Thread {
ArrayBlockingQueue<String> queue;
public Foodie(ArrayBlockingQueue<String> queue) {
this.queue = queue;
}
@Override
public void run() {
super.run();
}
}
public static void main(String[] args){
ArrayBlockingQueue<String> abq = new ArrayBlockingQueue<>(1);
Cook c = new Cook(abq);
Foodie f = new Foodie(abq);
c.start();
f.start();
}