ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

day18多线程

2021-08-04 22:01:50  阅读:181  来源: 互联网

标签:Thread System start 线程 println new 多线程 day18


  1. 有五个⼈同时过⼀个独⽊桥,⼀个独⽊桥同时只能允许⼀个⼈通过。每⼀个⼈通 过独⽊桥的时间是随机在 [5,10] 秒,输出这个独⽊桥上每⼀个⼈的通过详情,例 如:张三开始过独⽊桥了… 张三通过独⽊桥了!
public static void main(String[] args) {
        new Thread(()->{printMessage("张三");}).start();
        new Thread(()->{printMessage("李四");}).start();
        new Thread(()->{printMessage("王五");}).start();
        new Thread(()->{printMessage("赵六");}).start();
        new Thread(()->{printMessage("钱七");}).start();
    }
    public synchronized static void printMessage(String name){
        System.out.println(name+"开始过独⽊桥了");
        System.out.println(name+"通过独⽊桥了!");
    }
  1. 写出以下程序可能得执⾏结果
class MyThread extends Thread{
     public void run()
     {
         try {
             Thread.currentThread().sleep(3000);
         } catch (InterruptedException e) {
         }
         System.out.println("MyThread running");
     }
}
public class ThreadTest{
     public static void main(String argv[])
     {
         MyThread t = new MyThread();
         t.run();
         t.start();
         System.out.println("Thread Test");
     }
}

MyThread running

Thread Test

MyThread running

  1. 需求说明 ⼀个线程打印0-9的随机数字,⼀个线程打印随机的26个⼩写字⺟。
public static void main(String[] args) {
        Random random = new Random();
        new Thread(()->{
            for (int i = 0; i < 500 ; i++) {
                System.out.println(random.nextInt(10));
            }
        }).start();
        new Thread(()->{
            for (int i = 0; i < 500 ; i++) {
                System.out.println((char) ('a'+random.nextInt(26)));
            }
        }).start();

    }
  1. 需求说明 总经理今天很忙 任务清单: 开除那个不靠谱的副总经理; 给各部⻔总监开会; 陪VIP客 户吃饭,打牌,KTV,桑拿,按摩…; 去⾹港给妻⼦买个 华为⽜逼版 作为⽣⽇礼物; 去机场接 ⼥⼉送到公司旁边的希尔顿饭店休息; 陪⽼妈去医院看腰间盘突出; 辅导⼉⼦做作业 请帮助总经理⽤多线程的⽅式完成今天的任务
public static void main(String[] args) {
        new Thread(()->{
            System.out.println("开除那个不靠谱的副总经理;");
        }).start();
        new Thread(()->{
            System.out.println("给各部⻔总监开会;");
        }).start();
        new Thread(()->{
            System.out.println("陪VIP客户吃饭,打牌,KTV,桑拿,按摩...;");
        }).start();
        new Thread(()->{
            System.out.println("去⾹港给妻⼦买个 华为⽜逼版 作为⽣⽇礼物;");
        }).start();
        new Thread(()->{
            System.out.println("去机场接⼥⼉送到公司旁边的希尔顿饭店休息;");
        }).start();
        new Thread(()->{
            System.out.println("陪⽼妈去医院看腰间盘突出;");
        }).start();
        new Thread(()->{
            System.out.println("辅导⼉⼦做作业");
        }).start();
        
    }
  1. 定义⼀个线程A,输出1 〜 10之间的整数, 定义⼀个线程B,逆序输出1 〜 10之间的 整数,要求线程A和线程B交替输出
public class Work5 {
    static int i = 0;
    public static void main(String[] args) {
        Thread thread = new Thread(() -> print1());
        Thread thread1 = new Thread(() -> print2());
        thread.start();
        thread1.start();
    }
    public synchronized static void print1(){
        for (int j = 1; j < 11; j++) {
            try {
                if (i%2==0) {
                    System.out.println("print1= "+j);
                    i++;
                    Work5.class.notify();
                }
                else {
                    j--;
                    Work5.class.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public synchronized static void print2(){
        for (int j = 10; j >0; j--) {
            try {
                if (i%2==1) {
                    System.out.println("print2= "+j);
                    i++;
                    Work5.class.notify();
                }
                else {
                    j++;
                    Work5.class.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
  1. 当⼀个线程进⼊⼀个对象的⼀个synchronized⽅法后,其它线程是否可进⼊此对象 的其它⽅法?
     不能,⼀个对象的⼀个synchronized⽅法只能由⼀个线程访问。 
    
  2. 请说出你所知道的线程同步的⽅法。 wait():使⼀个线程处于等待状态,并且释放所持有的对象的lock。

    ​ sleep():使⼀个正 在运⾏的线程处于睡眠状态,是⼀个静态⽅法,调⽤此⽅法要捕捉 InterruptedException异常。

    ​ notify():唤醒⼀个处于等待状态的线程,注意的是在调 ⽤此⽅法的时候,并不能确切的唤醒某⼀个等待状态的线程,⽽是由JVM确定唤醒哪 个线程,⽽且不是按优先级。

    ​ Allnotity():唤醒所有处⼊等待状态的线程,注意并不是给所有唤醒线程⼀个对象的锁,⽽是让它们竞争。

  3. 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程 序。 注:因为这4个线程共享J,所以线程类要写到内部类中。 加线程:每次对j加 ⼀。 减线程:每次对j减⼀
public class Work8 {
    static int j = 0;
    static Lock lock = new ReentrantLock();
    static Condition conditionAdd = lock.newCondition();
    static Condition conditionSub = lock.newCondition();
    static boolean isAdd = true;

    public static void main(String[] args) {
        AddThread addThread1 = new AddThread();
        AddThread addThread2 = new AddThread();

        SubThread subThread1 = new SubThread();
        SubThread subThread2 = new SubThread();

        addThread1.start();
        addThread2.start();
        subThread1.start();
        subThread2.start();

    }
    static class AddThread extends Thread{
        @Override
        public void run() {
            lock.lock();
            for (int i = 0; i <= 10 ; i++) {
                if(isAdd){
                    j++;
                    System.out.println("add"+Thread.currentThread().getName()+" j="+j);
                    isAdd = !isAdd;
                    conditionSub.signal();
                }else {
                    try {
                        conditionAdd.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            conditionSub.signalAll();
            lock.unlock();
        }
    }
    static class SubThread extends Thread{
        @Override
        public void run() {
            lock.lock();
            for (int i = 0; i <= 10; i++) {
                if(!isAdd){
                    j--;
                    System.out.println("sub"+Thread.currentThread().getName()+" j="+j);
                    isAdd = !isAdd;
                    conditionAdd.signal();
                }
                else {
                    try {
                        conditionSub.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            conditionAdd.signal();
            lock.unlock();
        }
    }

}

标签:Thread,System,start,线程,println,new,多线程,day18
来源: https://blog.csdn.net/weixin_44481341/article/details/119394142

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有