老师 这样写能行吗?

老师 这样写能行吗?

https://img1.sycdn.imooc.com//climg/62ce61390950798210110768.jpg

https://img1.sycdn.imooc.com//climg/62ce61490935bd6f08540463.jpg

直接把图一要执行的代码 写在图二Run()方法能行吗?为啥非要绕一圈?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小黑 2022-07-13 16:23:05

同学你好,由于同学给出的都是代码片段,老师无法给出确切的解决方案。
如果同学的代码与课程源码一致,都是有锁对象Object lock的,那么此处是可以合并到一起的。解决方式是在run方法中使用synchronized代码块,锁对象为printer.lock即可。代码如下:

public class SyncSample {

    class Printer{
        //锁对象
        Object lock = new Object();
        //synchronized代码块演示,对自定义对象lock上锁
    }

    class PrintTask implements Runnable{
        public Printer printer;
        @Override
        public void run() {
            synchronized (printer.lock) {
                try {
                    //Printer.class
                    Thread.sleep(500);
                    System.out.print("魑");
                    Thread.sleep(500);
                    System.out.print("魅");
                    Thread.sleep(500);
                    System.out.print("魍");
                    Thread.sleep(500);
                    System.out.print("魉");
                    System.out.println();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void start(){
        Printer printer = new Printer();
        for(int i = 0 ; i < 10 ; i++){
            PrintTask task = new PrintTask();
            task.printer = printer;
            Thread thread = new Thread(task);
            thread.start();
        }
    }

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

祝学习愉快!

  • 提问者 星辰很精彩 #1
    class SynTask implements Runnable {
                //public printer printer;
                @Override
                public synchronized  void  run() {
                    //printer.print2();
                    try {
                        Thread.sleep(500);
                        System.out.print("魑");
                        Thread.sleep(500);
                        System.out.print("魅");
                        Thread.sleep(500);
                        System.out.print("魍");
                        Thread.sleep(500);
                        System.out.print("魉");
                        System.out.println();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
    
        }
    }
    public void start() {
        //printer pr = new printer();
        for (int i = 1; i < 5; i++) {
            SynTask Task = new SynTask();
            //Task.printer = pr;
            Thread thread = new Thread(Task);
            Task.run();
            thread.start();
        }
    }
    public static void main(String[] args) {
        SynSample value = new SynSample();
        value.start();
    }

    老师 这打印有问题 我该如何修改啊?

    2022-07-13 17:00:47
  • 好帮手慕小黑 回复 提问者 星辰很精彩 #2

    同学你好,上面老师理解错误,已经对回答进行了更改,请同学查看。

    祝学习愉快!

    2022-07-13 17:33:06
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师