Java多线程

2021-05-06 04:29

阅读:644

标签:voice   pop   线程   dex   同步   方式   重写   lam   throw   

Java多线程

实现多线程的几种方式

继承Thread类

  • 自定义线程类继承Thread类
  • 重写run()方法
  • 创建线程对象,调用start()方法启动线程
public class MyThread entends Thread {
    @Override
    public void run() {
        // ...
    }
}

public static void main(String[] args) {
    MyThread mt = new MyThread();
    mt.start();
}

实现Runnable接口

  • 实现Runnable接口
  • 实现run()方法
  • 创建线程对象,调用start()方法
public class MyRunnable implements Runnable {
    @override
    public void run() {
        // ...
    }
}

public static main(String[] args) {
    MyRunnable mr = new MyRunnable();
    new Thread(mr).start();
}
Lambda表达式
new Thread(()->{
    // ...
}).start();

线程的状态:创建、就绪、阻塞、运行、死亡

stop:使用标志位

public class testStop implements Runnable {
    // 标志位
    private boolean flag = true;
    @Override
    public void run() {
        while(flag) {
            System.out.println("running ");
        }
    }
    // 提供标示
    public void stop() {
        this.flag = false;
    }

    public static void main(String[] args) {
        testStop ts = new testStop();
        new Thread(ts).start();
        // 主线程
        for(int i = 1; i 

sleep: 线程休眠, 线程阻塞的毫秒数

  • 每个对象都有一个锁,sleep不会释放锁。
public class testSleep {
    // sleep 存在异常InterruptedException
    public static void countDown() throws InterruptedException {
        int n = 10;
        while(n > 0) {
            Thread.sleep(100);
            n--;
        }
    }
    public static void main(String[] args) {
        Date time = new Date(System.currentTimeMillis());
        try {
            System.out.println(new SimpleDateFormat("hh:mm:ss").format(System.currentTimeMillis()));
            countDown();
            time = new Date(System.currentTimeMillis());
            System.out.println(new SimpleDateFormat("hh:mm:ss").format(System.currentTimeMillis()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

yield: 线程礼让,当前正在执行的线程暂停,不阻塞,让CPU重新调度

public class testYield {
    public static void main(String[] args) {
        MyYield my = new MyYield();
        new Thread(my,"a").start();
        new Thread(my, "b").start();
    }
}

class MyYield implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + " at work");
        Thread.yield();
        System.out.println(Thread.currentThread().getName() + " out of work");
    }
}

Join: 线程合并,先执行此线程,其他线程阻塞

public class testJoin {
    public static void main(String[] args) throws InterruptedException {
        MyJoin mj = new MyJoin();
        Thread td = new Thread(mj);
        td.start();

        for(int i = 0; i 

线程同步:

第一个线程访问,锁定同步监视器,执行其中的代码
第二个线程访问,发现同步监视器被锁定,无法访问
第一个线程访问完毕,解锁同步监视器
第二个线程访问,锁定访问

  • synchronized:同步方法
  • synchronize(Obj):同步块

生产者/消费者模式

方式一:管程法

public class testPC {
    public static void main(String[] args) {
        SynContainer container = new SynContainer();
        new Productor(container).start();
        new Consumer(container).start();
    }

}

class Productor extends Thread {
    SynContainer container;
    public Productor(SynContainer container) {
        this.container = container;
    }
    public void run() {
        for(int i = 0; i " + container.pop().id + "只鸡");
        }
    }
}
class Chicken {
    int id;
    public Chicken(int id) {
        this.id = id;
    }
}

class SynContainer {
    Chicken[] chickens = new Chicken[10];
    int count = 0;
    public synchronized void push(Chicken chicken) {
        if (count == chickens.length) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        chickens[count] = chicken;
        count++;
        this.notifyAll();
    }
    public synchronized Chicken pop() {
        if(count == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        count--;
        Chicken chicken =  chickens[count];
        return chicken;
    }
}

方式二:信号灯法,标志位

public class testPC2 {
    public static void main(String[] args) {
        Tv tv = new Tv();
        new Player(tv).start();
        new Watcher((tv)).start();
    }
}

class Player extends Thread {
    Tv tv;
    public Player(Tv tv) {
        this.tv = tv;
    }
    public void run() {
        for (int i = 0; i 

线程池

  • 传入Runnable接口
public class testpool {
    public static void main(String[] args) {
        // 参数为线程池中线程个数
        ExecutorService ser = Executors.newFixedThreadPool(10);

        ser.execute(new MyThread());
        ser.execute(new MyThread());
        ser.execute(new MyThread());
        ser.execute(new MyThread());
        // 关闭
        ser.shutdown();
    }
}
class MyThread implements Runnable {
    @Override
    public void run() {
        for(int i = 0; i 

Java多线程

标签:voice   pop   线程   dex   同步   方式   重写   lam   throw   

原文地址:https://www.cnblogs.com/Hot-machine/p/13190348.html


评论


亲,登录后才可以留言!