• Java之多线程(6个demo)


    本文主要介绍Java中多线程,在Java中启动多线程的方法包括:继承Thread类或者实现Runnable接口,介绍了设置线程名称,设置线程优先级,设置守护线程等,介绍了线程同步,使用synchronized关键字和lock锁分别实现线程同步,解决线程安全问题,介绍了生产者和消费者模式,涉及线程延迟,等待,唤醒的方法。

    目录

    1-继承Thread类并重写run()方法实现多线程

    2-线程控制(设置主线程和守护线程)

    3-实现Runnable接口并重写run()方法实现多线程

    4-三个线程实现窗口卖票的案例(synchronized实现线程同步)

    5-Lock锁的方式是实现线程同步

    6-生产者与消费者模式案例


    1-继承Thread类并重写run()方法实现多线程

    1. public class MyThread extends Thread{
    2. @Override
    3. public void run() {
    4. for(int i=0; i<50; i++) {
    5. System.out.println(Thread.currentThread().getName() + ":执行了");
    6. }
    7. }
    8. }
    1. public class ThreadDemo01 {
    2. public static void main(String[] args) {
    3. MyThread myThread = new MyThread() ;
    4. MyThread myThread1 = new MyThread() ;
    5. //设置线程名
    6. myThread.setName("线程1");
    7. myThread1.setName("线程2");
    8. //设置线程优先级,线程 优先级默认是5,最小是1,最大是10
    9. myThread.setPriority(1);
    10. myThread1.setPriority(10);
    11. myThread.start();
    12. myThread1.start();
    13. }
    14. }

    2-线程控制(设置主线程和守护线程)

    1. public class MyThread1 extends Thread {
    2. @Override
    3. public void run() {
    4. for(int i=1; i<=50; i++){
    5. System.out.println(Thread.currentThread().getName() + "执行了:" + i);
    6. }
    7. }
    8. }
    1. public class ThreadDemo02 {
    2. public static void main(String[] args) {
    3. MyThread1 my1 = new MyThread1() ;
    4. MyThread1 my2 = new MyThread1() ;
    5. my1.setName("关羽");
    6. my2.setName("张飞");
    7. Thread.currentThread().setName("刘备"); //主线程
    8. //设置为守护线程,当全部为守护线程时,线程终止
    9. my1.setDaemon(true);
    10. my2.setDaemon(true);
    11. my1.start();
    12. my2.start();
    13. for(int i=1; i<=50; i++){
    14. System.out.println(Thread.currentThread().getName() + "执行了:" + i);
    15. }
    16. }
    17. }

    3-实现Runnable接口并重写run()方法实现多线程

    1. public class MyRunnable implements Runnable{
    2. @Override
    3. public void run() {
    4. for(int i=0; i<50; i++){
    5. System.out.println(Thread.currentThread().getName() + "执行了:" + i);
    6. }
    7. }
    8. }
    1. public class ThreadDemo03 {
    2. public static void main(String[] args) {
    3. MyRunnable myRunnable = new MyRunnable() ;
    4. Thread t1 = new Thread(myRunnable, "线程1") ;
    5. Thread t2 = new Thread(myRunnable, "线程2") ;
    6. t1.start();
    7. t2.start();
    8. }
    9. }

    4-三个线程实现窗口卖票的案例(synchronized实现线程同步)

    1. public class SellTickets implements Runnable {
    2. private static int tickets = 100 ; //初始化票数
    3. @Override
    4. public void run() {
    5. while(true) {
    6. //synchronized (this) {
    7. synchronized (SellTickets.class) {
    8. if (tickets > 0) {
    9. try {
    10. Thread.sleep(100); //卖票延迟时间
    11. } catch (InterruptedException e) {
    12. e.printStackTrace();
    13. }
    14. System.out.println(Thread.currentThread().getName() + "正在卖第" + tickets + "张票");
    15. tickets--;
    16. }
    17. }
    18. // }
    19. }
    20. }
    21. }
    1. public class ThreadDemo04 {
    2. public static void main(String[] args) {
    3. SellTickets sellTickets = new SellTickets() ;
    4. Thread t1 = new Thread(sellTickets,"窗口1") ;
    5. Thread t2 = new Thread(sellTickets, "窗口2") ;
    6. Thread t3 = new Thread(sellTickets, "窗口3") ;
    7. t1.start();
    8. t2.start();
    9. t3.start();
    10. }
    11. }

    5-Lock锁的方式是实现线程同步

    1. import java.util.concurrent.locks.Lock;
    2. import java.util.concurrent.locks.ReentrantLock;
    3. public class SellTickets implements Runnable {
    4. private static int tickets = 100 ; //初始化票数
    5. private Lock lock = new ReentrantLock() ;
    6. @Override
    7. public void run() {
    8. while(true) {
    9. //synchronized (this) {
    10. //synchronized (SellTickets.class) {
    11. lock.lock();
    12. if (tickets > 0) {
    13. try {
    14. Thread.sleep(100); //卖票延迟时间
    15. } catch (InterruptedException e) {
    16. e.printStackTrace();
    17. }
    18. System.out.println(Thread.currentThread().getName() + "正在卖第" + tickets + "张票");
    19. tickets--;
    20. }
    21. lock.unlock();
    22. // }
    23. // }
    24. }
    25. }
    26. }
    1. public class ThreadDemo04 {
    2. public static void main(String[] args) {
    3. SellTickets sellTickets = new SellTickets() ;
    4. Thread t1 = new Thread(sellTickets,"窗口1") ;
    5. Thread t2 = new Thread(sellTickets, "窗口2") ;
    6. Thread t3 = new Thread(sellTickets, "窗口3") ;
    7. t1.start();
    8. t2.start();
    9. t3.start();
    10. }
    11. }

    6-生产者与消费者模式案例

    牛奶箱类:

    1. public class Box {
    2. private int milk ;
    3. private static boolean state = false ;
    4. public synchronized void put(int milk) throws InterruptedException {
    5. if(state){ //有牛奶,等待消费者消费
    6. wait();
    7. }
    8. this.milk = milk;
    9. System.out.println("生产者将第" + this.milk + "瓶牛奶放入箱子中");
    10. state = true ;
    11. notifyAll(); //唤醒
    12. }
    13. public synchronized void get() throws InterruptedException {
    14. if(!state){ //没有牛奶,等待生产
    15. wait();
    16. }
    17. System.out.println("消费者从箱子中拿走了第" + this.milk + "瓶牛奶");
    18. state = false ;
    19. notifyAll(); //唤醒
    20. }
    21. }

    生产者类:

    1. public class Producer implements Runnable {
    2. private Box box ;
    3. public Producer(Box box) {
    4. this.box = box;
    5. }
    6. @Override
    7. public void run() {
    8. for(int i=1; i<=10; i++){
    9. try {
    10. box.put(i);
    11. } catch (InterruptedException e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. }
    16. }

    消费者类:

    1. public class Customer implements Runnable {
    2. private Box box ;
    3. public Customer(Box box) {
    4. this.box = box;
    5. }
    6. @Override
    7. public void run() {
    8. while(true){
    9. try {
    10. box.get();
    11. } catch (InterruptedException e) {
    12. e.printStackTrace();
    13. }
    14. }
    15. }
    16. }

    测试类:

    1. public class BoxDemo {
    2. public static void main(String[] args) {
    3. Box box = new Box() ;
    4. Producer producer = new Producer(box) ;
    5. Customer customer = new Customer(box) ;
    6. Thread t1 = new Thread(producer) ;
    7. Thread t2 = new Thread(customer) ;
    8. t1.start();
    9. t2.start();
    10. }
    11. }
  • 相关阅读:
    【前端必会】不知道webpack插件? webpack插件源码分析BannerPlugin
    DataScience&ML:基于心脏病分类预测数据集利用等算法实现模型可解释性之详细攻略
    springboot 获取参数
    Linux -开机、重启和用户登录注销
    numpy数组(无冒号,单冒号,双冒号)的含义
    leetcode:207. 课程表
    redis的redis.service配置
    【线性代数】为什么 AA* = |A|E
    redis为什么要自己实现SDS表示字符串
    HVV(护网)蓝队视角的技战法分析
  • 原文地址:https://blog.csdn.net/nuist_NJUPT/article/details/126199787