day18多线程

  1. 有五个⼈同时过⼀个独⽊桥,⼀个独⽊桥同时只能允许⼀个⼈通过。每⼀个⼈通 过独⽊桥的时间是随机在 [5,10] 秒,输出这个独⽊桥上每⼀个⼈的通过详情,例 如:张三开始过独⽊桥了… 张三通过独⽊桥了!
public class Work1 {
    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){
        int i = new Random().nextInt(5)+5;
        System.out.println(name+"开始过独⽊桥了");
        try {
            Thread.sleep(i*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        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();
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值