Java学习之路-第五周小结-下

文章介绍了进程的独立性和互斥性,以及线程的特性,包括抢占式运行和资源共享。通过继承Thread、实现Runnable和Callable接口展示了三种创建线程的方式。提到了线程同步的同步方法和同步代码块,以及死锁的概念和示例。最后讨论了等待唤醒机制,如Object类的wait(),notify(),notifyAll()方法,以及ArrayBlockingQueue作为阻塞队列在生产者消费者模型中的应用。
摘要由CSDN通过智能技术生成

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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值