2021-05-19

课堂代码

public class Demo01 extends Thread {
    public Demo01() {
    }

    public Demo01(String name) {
        super(name);
    }

    public void run() {
        System.out.println("这是一个新的线程,自己实现的");
        System.out.println("名字:" + Thread.currentThread().getName());
    }
}

public class TestDemo01 {
    public TestDemo01() {
    }

    public static void main(String[] args) {
        Demo01 demo01 = new Demo01("1");
        Demo01 demo02 = new Demo01("2");
        Demo01 demo03 = new Demo01("3");
        Demo01 demo04 = new Demo01("4");
        demo01.setPriority(1);
        demo02.setPriority(3);
        demo03.setPriority(7);
        demo04.setPriority(10);
        demo01.start();
        demo02.start();
        demo03.start();
        demo04.start();
    }
}

public class Demo02 implements Runnable {
    public Demo02() {
    }

    public void run() {
        System.out.println("新线程,实现接口的方式");
    }
}

public class TestDemo02 {
    public TestDemo02() {
    }

    public static void main(String[] args) {
        Demo02 demo02 = new Demo02();
        Thread thread = new Thread(demo02);
        thread.start();
    }
}

public class DamemonThread implements Runnable {
    public DamemonThread() {
    }

    public void run() {
        System.out.println("进入守护线程" + Thread.currentThread().getName());
        System.out.println("守护线程开工了.....");
        this.writeToFile();
        System.out.println("退出守护线程" + Thread.currentThread().getName());
    }

    public void writeToFile() {
        for(int count = 0; count < 999; ++count) {
            System.out.println("守护线程" + Thread.currentThread().getName() + count);
        }

    }
}

public class TestDemo03 {
    public TestDemo03() {
    }

    public static void main(String[] args) throws InterruptedException {
        System.out.println("进入主线程" + Thread.currentThread().getName());
        DamemonThread damemonThread = new DamemonThread();
        Thread thread = new Thread(damemonThread);
        thread.setDaemon(true);
        thread.start();
        Thread.sleep(500L);
        System.out.println("你好,世界");
        System.out.println("退出主线程" + Thread.currentThread().getName());
    }
}

public class Demo04 {
    public Demo04() {
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                System.out.println(Thread.currentThread().getName() + "卖出1张票,还剩:" + --TickerContent.count + "张");
            }
        };
        (new Thread(r, "1")).start();
        (new Thread(r, "2")).start();
        (new Thread(r, "3")).start();
        (new Thread(r, "4")).start();
        (new Thread(r, "5")).start();
        (new Thread(r, "6")).start();
    }
}

public class TickerContent {
    public static int count = 50;

    public TickerContent() {
    }
}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 中老年摄影教学改革初探 由于爱好...
    李老头阅读 1,252评论 0 0
  • 在信息技术2.0的实施工作中,按照学校要求,结合本教研组制定的计划和本人的特点,积极参与,认真学习,不断尝试。这是...
    浅浅慢慢阅读 847评论 0 1
  • 智力背景的阅读 ——《给教师的一百条建议》阅读随笔三 //行言 20210415 苏霍姆林斯基建议:所有的教师,为...
    行言xy阅读 3,707评论 0 0
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 12,758评论 28 53
  • 信任包括信任自己和信任他人 很多时候,很多事情,失败、遗憾、错过,源于不自信,不信任他人 觉得自己做不成,别人做不...
    吴氵晃阅读 11,360评论 4 8

友情链接更多精彩内容