java多线程

多线程实现方式

继承Thread类

流程

1.定义一个类继承Thread,

2.重写run方法

3.创建对象,调用start方法;开启线程

代码
继承Thread类,重写run方法
public class MyThread extends Thread{
    public static void main(String[] args) {

    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+"hello,world");
        }
    }
}
创建线程,调用start对象创建线程

public class Main {
    public static void main(String[] args) {
        MyThread t1=new MyThread();
        MyThread t2=new MyThread();
        MyThread t3=new MyThread();
        t1.setName("线程一");
        t2.setName("线程二");
        t3.setName("线程三");
        t1.start();;
        t3.start();
        t2.start();
    }
}

 实现Runnable接口

流程

1.实现Runnable接口

2.重写run方法

3.创建自己的类

4.创建Thread类,传递自己的类;

5.启动run方法

代码
public class MyThread1 implements  Runnable{
    @Override
    public void run() {
        for (int i = 0; i <10 ; i++) {
            Thread t=Thread.currentThread();
            System.out.println(t.getName()+"mike");
        }
    }
}
 Thread t7=new Thread(t6);

        Thread t5=new Thread(t4);

        t5.setName("线程5");
        t7.setName("线程7");

        t5.start();
        t7.start();

利用Callable接口和Future接口实现

Callable和FutureTask可以返回线程的结果,并对线程进行管理;

Callable可以返回结果,FutureTask可以对线程进行管理,

流程

流程:创建Callable对象->Callable对象作为FutureTask对象的构造参数,创造FutureTask对象,->将FutureTask对象作为参数创建Thread对象->Thread对象调用start启动线程->FutureTask对象调用get方法返回线程的结果值;

代码

public class MyCallable implements Callable<Integer> {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //可以返回现成的结果;
        MyCallable ms=new MyCallable();

        //利用futuretask管理线程;
        FutureTask<Integer>ft=new FutureTask<>(ms);

        Thread t=new Thread(ft);

        t.start();
        Integer res=ft.get();
        System.out.println(res);
    }
    @Override
    public Integer call() throws Exception {
        int sum=0;
        for (int i = 0; i < 10; i++) {
            sum+=i;
        }
        return sum;
    }//表示返回结果的类型


}

三种方式的不同

前两种无法返回线程的结果,第三种可以,推荐第三种。

Thread类的方法

常见成员方法

sleep方法:将会让当前线程休眠,在main方法里就是main线程

Thread.currentThread:获取当前线程的对象;

  public static void main(String[] args) throws InterruptedException {
        //将获取当前线程对象;
        Thread t=Thread.currentThread();
        Thread.sleep(5000);
        System.out.println(t.getName());
    }

setpriority(int newpriority)

设置当前线程的优先级,默认为5,最高10,最低1

int getpriority()

返回当前线程的优先级

void setDaemon(boolean on);设置守护线程

守护线程的生命周期与非守护线程 一样,非守护线程结束,守护线程也会结束;

yield()

当前线程将会让出CPU的执行权,但下一次仍然会抢夺;

join()

将线程插入到当前线程之前,只有线程执行完后,当前线程才会执行;

public class MyThread5 extends Thread{
    public MyThread5() {
    }

    public MyThread5(String name) {
        super(name);
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread5 t=new MyThread5("女神");
      //  t.setPriority(9);
        MyThread3 t1=new MyThread3("备胎");
      //  t1.setPriority(6);
        t1.setDaemon(true);
        t1.start();
        t.start();
        t.join();
     int i=   Thread.currentThread().getPriority();
        System.out.println(i);
        for (int j = 0; j <11 ; j++) {
            System.out.println(j);
        }
    }

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

线程的状态

synchronized是一个bool量,表示两种状态,一个线程获得锁后,量的值改变,其余线程都无法获得。

LOCK锁

等待唤醒机制

 锁对象作为消息载体来管理线程,如唤醒,睡眠不同的线程,不同的线程使用相同的锁对象来传递消息。

下图中使用 desk.lock作为锁,通过desk.lock调用wait,notifyAll方法,来唤醒与lock相关的线程,注意,多个线程应该绑定一把锁,所以lock采用静态对象,来传递消息;

public class desk {
    public static int floodflag=0;
    public static int count=10;
    public static Object lock=new Object();
}
public class Foodie extends Thread{


    public Foodie(String name) {
        super(name);
    }

    @Override
    public void run() {
        while(true){
            synchronized (desk.lock){
                if(desk.count==0){
                    break;
                }
                else{
                    //判断资源
                    //唤醒;
                    //
                    if(desk.floodflag==0){
                        try {
                            //休眠与唤醒
                            //当前线程休眠
                            desk.lock.wait();
                            //唤醒与lock相关的线程
                            desk.lock.notifyAll();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else{
                        desk.count--;
                        System.out.println("还能吃"+desk.count+"碗!");
                        //唤醒lock相关的线程
                        desk.lock.notifyAll();

                        desk.floodflag=0;
                    }
                }
            }
        }
    }
public class Cook extends Thread{
    public Cook(String name) {
        super(name);
    }

    public static void main(String[] args) {
        Cook c=new Cook("吃货");
        Foodie f=new Foodie("厨师");
       c.start();
       f.start();
    }

    @Override
    public void run() {
        while (true){
            synchronized (desk.lock){
                if(desk.count==0){
                    break;
                }
                else{
                    if(desk.floodflag==1){
                        //用锁对象调用wait;使用锁对象绑定
                        try {
                            desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    else{
                        System.out.println("厨师做了一碗面条");
                        desk.floodflag=1;
                        //唤醒吃货开吃
                        desk.lock.notifyAll();
                    }
                }
            }
        }
    }

 阻塞队列

阻塞队列有ArrayBlockingQueue和LinkedBlockingQueue两种实现方式,前者用数组实现,后者用链表实现;

注意生产者消费者使用同一个阻塞队列,分别使用take,put方法拿走,放入数据;

线程的六种状态

线程的状态转换采用以下函数获得:

 

线程池

 

 

自定义线程池

,参数

 线程池多大合适

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值