java项目总结9

目录

1.多线程

1.什么是多线程

2.并发和并行

3.多线程的实现方式

1.继承Thread类的方式进行实现

2.实现Runnable接口的方式进行实现

3.利用Callable接口方式和Ftrue接口方式实现

4.多线程的常用方法

1.线程的优先级

2.守护线程

3.礼让线程

4.插入线程

5.线程的生命周期

​编辑

6.线程的安全问题

7.同步方法

8.Lock锁

9.等待唤醒机制(生产者和消费者)

10.阻塞队列实现等待唤醒进制(单列集合)

11.多线程的六种状态

12.线程池


1.多线程

1.什么是多线程

2.并发和并行

并发:一对多

并行:多对多

3.多线程的实现方式

1.继承Thread类的方式进行实现

2.实现Runnable接口的方式进行实现

3.利用Callable接口方式和Ftrue接口方式实现

小结:

4.多线程的常用方法

public class Main {
    public static void main(String[] args) {

//        Swim0 t1=new Swim0("yan");
//        Swim0 t2=new Swim0("qqq");
//
//        t1.start();
//        t2.start();


        Thread t=Thread.currentThread();
        String name = t.getName();
        System.out.println(name);
    }
}
//
public  class Swim0 extends Thread {
    public  Swim0(){};
    public Swim0(String name){
        super(name);
    }


    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+"="+i);
        }
    }
}

1.线程的优先级

这里setPriority函数是设置优先级,分为1到10,数字越大分配到的概率越大,系统默认都是5,包括main。


public class Main {
    public static void main(String[] args) {

        Swim0 t1 = new Swim0("颜");
        Swim0 t2 = new Swim0("qqq");

        t1.setPriority(1);
        t2.setPriority(10);

        t1.start();
        t2.start();


    }
}
///
public  class Swim0 extends Thread {
    public  Swim0(){};
    public Swim0(String name){
        super(name);
    }


    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName()+"="+i);
        }
    }
}

2.守护线程

当非守护线程执行完成后,守护线程也会自动停止。

运用场景:(聊天:非守护,传输文件:守护)

3.礼让线程

让线程运行结果尽可能均匀一点(非重点)

4.插入线程

非重点

5.线程的生命周期

6.线程的安全问题

如下;

用来写线程方法的类:

衍生:同步代码块

加锁:(让线路轮流执行,不会因为线程1在休眠的,线程2,3来抢导致输出错误)

细节:

1.synchronized必须要写在while的后面,否则这个方法就一直是第一个线程在执行了。

2.锁对象一定的是唯一的。唯一化(静态变量 static)

一般写当前字节码文件对象(假设当前类名为MyThread)(这里可以用MyThread.class代替obj)

7.同步方法

还是上面的题:(上面用的是多线程的第一种实现方法,继承类型的,所以ticket要加static,这里用的是第二种,接口类型的)

将其同步方法化:选住方法体,按Alt+Ctrl+m即可快速生成函数

8.Lock锁

9.等待唤醒机制(生产者和消费者)

完整规则如下:A,B线程

测试:

public class Main {
    public static void main(String[] args) {

  Cook c=new Cook();
  Foodie f=new Foodie();

  c.setName("神厨小福贵");
  f.setName("小胖子");

  c.start();
  f.start();
  
    }
}
///
// 桌子类
public class Desk {
    public static int count=10;
    public static int foodFlag=0;
    public static Object lock=new Object();



}
///
//小胖子(吃)
public class Foodie extends Thread {
    @Override
    public void run() {

        while (true) {
            synchronized (Desk.lock) {
                if (Desk.count == 0)
                    break;
                else {
                    if (Desk.foodFlag == 0) {
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        Desk.count--;
                        System.out.println("小胖子在吃面条,还能吃" + Desk.count + "碗") ;
                        Desk.lock.notifyAll();
                        Desk.foodFlag = 0;
                    }
                }
            }
        }
    }
}

//厨师(做饭)
public class Cook extends Thread
{
    @Override
    public void run() {
        while(true){
            synchronized(Desk.lock){
                if(Desk.count==0)
                    break;
                else{
                    if(Desk.foodFlag==1){
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    else{
                        System.out.println("厨师做好了一碗面条");
                        Desk.foodFlag=1;
                        Desk.lock.notifyAll();
                    }
                }
            }
        }
    }
}

10.阻塞队列实现等待唤醒进制(单列集合)

都是接口,需要创建他们实现类的对象。

测试如下:

public class test {
    public static void main(String[] args) {
//1.创建阻塞队列的对象
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);

        //2.创建线程的对象,传递阻塞队列
        Cook  c=new Cook(queue);
        Foodie f=new Foodie(queue);

        c.start();
        f.start();


    }
}
/
    ArrayBlockingQueue<String> queue;
    public Cook(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                queue.put("面条");
                System.out.println("厨师放了一碗面");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
//
public class Foodie extends Thread {
    ArrayBlockingQueue<String> queue;
    public Foodie(ArrayBlockingQueue<String> queue){
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
            String str = queue.take();
                System.out.println(str);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

11.多线程的六种状态

运行状态除外

12.线程池

掌握核心原理即可。

线程池

解释:

最大并行数:

计算自己电脑的是几核的?

public class gao {
    public static void main(String[] args) {
        int count=Runtime.getRuntime().availableProcessors();
        System.out.println(count);
    }
}

my 电脑is 20!!!

多线程额外拓展

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值