Java Thread/Runnable线程基础

写在前面

一般 Java 开发中,Java 线程因为使用场景少,里面的对象敏感而抽象,学习起来,比较吃力…
线程的创建比较重要,不同的创建方式,好的创建方式,可更好的管理线程,状态,监控等…我们的线程不该是 Hello World不是简单的 new 就可以…

一、五种创建方式

1.1、

import java.util.concurrent.TimeUnit;

public class CreatingThreadsFirstWay {
   
    public static void main (String[] args) {
   
        System.out.println("Starting main!!!!");
        new FirstWay();
        Thread thread = new FirstWay();
        System.out.println("Ending main!!!");
    }
}

class FirstWay extends Thread {
   

    private static int count = 1;
    private int id;

    @Override
    public void run() {
   
        System.out.println("Starting First Way -------");
        for(int i = 1; i <= 10; i++) {
   
            System.out.println("<"+ id + "> Count: " +i);
            try {
   
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
        }
        System.out.println("Ending First Way -------");
    }

    public FirstWay() {
   
        id = count++;
        this.start();
    }
}

1.2、

public class CreatingThreadsSecondWay {
   
    public static void main (String[] args) {
   
        System.out.println("Starting main!!!!");

// new SecondWay().start();

        Thread thread = new FirstWay();
        thread.start();

        System.out.println("Ending main!!!");
    }
}

class SecondWay extends Thread {
   

    private static int count = 1;
    private int id;

    @Override
    public void run() {
   
        System.out.println("Starting First Way -------");
        for(int i = 1; i <= 10; i++) {
   
            System.out.println("<"+ id + "> Count: " +i);
            try {
   
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
        }
        System.out.println("Ending First Way -------");
    }

    public SecondWay() {
   
        id = count++;
    }
}

1.3、

public class CreatingThreadsThirdWay {
   
    public static void main (String[] args) {
   
        System.out.println("Starting main.......");
        new ThirdWay();
        new ThirdWay();
        System.out.println("Ending main.......");
    }
}

class ThirdWay implements Runnable {
   

    static int count = 1;
    int id = 0;

    public void run() {
   
        for(int i = 1 ; i <= 10; i++) {
   
            try {
   
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
            System.out.println("<"+id+"> Tick Tick: " + i);
        }
    }


    ThirdWay() {
   
        this.id = count++;
        new Thread(this).start();
    }
}

1.4、

public class CreatingThreadsFourthWay {
   
    public static void main (String[] args) {
   
        System.out.println ("Starting Main..........");

        ExecutorService executorService = Executors.newFixedThreadPool(2);

        executorService.submit(new FourthWay());
        executorService.submit(new FourthWay());

        System.out.println ("Ending Main..........");
    }
}

class FourthWay implements Runnable {
   

    // 用来监视线程数量
    static int count = 1;
    int id = 0;

    public void run() {
   
        for(int i = 1 ; i <= 10; i++) {
   
            try {
   
                TimeUnit.MILLISECONDS.sleep(200);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
            System.out.println("<"+id+"> Tick Tick: " + i);
        }
    }

    FourthWay() {
   
        this.id = count++;
    }
}

1.5、

public class CreatingThreadsFifthWay {
   
    public static void main (String[] args) {
   
        System.out.println("Starting Main........");
        Thread t = new Thread(new Runnable() {
   
            public void run() {
   
                for(int i = 1 ; i <= 10; i++) {
   
                    try {
   
                        TimeUnit.MILLISECONDS.sleep(200);
                    } catch (InterruptedException e) {
   
                        e.printStackTrace();
                    }
                    System.out.println("Tick Tick: " + i);
                }
            }
        });
        t.start();
        System.out.println("Ending Main........");
    }
}

二、比较区别

待记录

全部评论

相关推荐

就前几天旅游的时候,打开抖音就经常刷到这类视频:以前是高学历学生、老师、主持人,现在做着团播、擦边主播的工作,以及那些经过精心包装的“职业转型”故事——从铺天盖地的VLOG到所谓的“04年夜场工作日记”,这些内容在初中升学、高考放榜等关键时间节点持续发酵。可以说非常直接且精准地在潜移默化地影响着心智尚未成熟的青少年,使其对特殊行业逐渐脱敏。那我就想问了:某些传播公司、平台运营者甚至某些夜场的老板,你们究竟在传递怎样的价值观?点开那些视频,评论区里也是呈现明显的两极分化:一种是​​经济下行论​​:“现在就业市场已经艰难到这种程度了吗?”​​一种是事实反驳派​​:这些创作者往往拥有名校背景,从事着...
牛客刘北:被环境教育的,为了能拿到足够的钱养活自己,不甘心也得甘心,现在的短视频传播的思想的确很扭曲,但是很明显,互联网玩上一年你就能全款提A6,但你全心全意不吃不喝工作一年未必能提A6,但是在高考中考出现这个的确很扭曲,在向大家传播“不上学,玩互联网也可以轻松年入百万”,不是人变了,是社会在变
预测一下26届秋招形势
点赞 评论 收藏
分享
qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务