Runnable接口详细详解

  1. 创建线程对象,默认有一个线程名,以Thread-开头,从0开始计数

    构造函数Thread()

    Thread-0

    Thread-1

    Thread-2

    其他构造方法

    Thread(Runnable target)

  2. 如果在构造thread的时候没有传递Runnable或者没有复写Thread的run方法,该thread将不会

    调用任何的东西,如果传递了Runnable接口的实例,后者复写了Thread的run方法,则会执行该

    方法的逻辑单元(逻辑代码)

    public class CreateThread2 {
        public static void main(String[] args) {
            Thread t1 = new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t1.start();
            System.out.println(Thread.currentThread().getName());
    
        }
    }
    
  3. 如果构造线程对象时未传入ThreadGroup,Thread会默认获取父线程的ThreadGroup作为该

    该线程的ThreadGroup,此时子线程和父线程将会在同一个threadGroup中.

    public class CreateThread2 {
        public static void main(String[] args) {
            Thread t1 = new Thread() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            t1.start();
            System.out.println(Thread.currentThread().getName());
    
            ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
    
            System.out.println(threadGroup.activeCount());
            Thread[] threads = new Thread[threadGroup.activeCount()];
            threadGroup.enumerate(threads);
            Arrays.asList(threads).forEach(System.out::println);
    
        }
    }
    
  4. 构造Thread的时候传入stacksize代表着该线程占用的stack大小,如果没有指定stacksize

    的大小,默认是0,0代表着会忽略该参数,该参数会被JNI函数去使用

    需要注意:该参数有一些平台有效,有些平台无效

    /**
     *@ClassName:CreateThread3
     *@Author:linianest
     *@CreateTime:2020/3/14 11:32
     *@version:1.0
     *@Description TODO: 将数据压进栈
     */
    public class CreateThread3 {
    
        private static int i =0;
        private static int counter=0;
        private byte[] bytes = new byte[1024];
    
        // JVM will create a thread named main.
        public static void main(String[] args) {
            // create a JVM stack
    
            // 栈操作,压栈
            try {
                add(0);
            } catch (Error e) {
                e.printStackTrace();
                System.out.println(counter);
            }
        }
        public static void add(int i){
            ++counter;
            add(i+1);
        }
    }
    //java.lang.StackOverflowError
    //26401
    

    测试出我的平台jvm的大小是26401,线程基本没有宽度

    /**
     * @ClassName:CreateThread5
     * @Author:linianest
     * @CreateTime:2020/3/14 15:46
     * @version:1.0
     * @Description TODO: 调整线程宽度
     */
    public class CreateThread5 {
        private static int counter = 1;
    
        public static void main(String[] args) {
            try {
                for (int i = 0; i < Integer.MAX_VALUE; i++) {
                    counter++;
                    new Thread(() -> {
                        byte[] data = new byte[1024 * 1024 * 2];
                        while (true) {
    //                        try {
    //                            Thread.sleep(1);
    //                        } catch (InterruptedException e) {
    //                            e.printStackTrace();
    //                        }
                        }
                    }).start();
                }
            } catch (Error e) {
            }
    
            System.out.println("Total created thread nums=>" + counter);
        }
    }
    
    

最后说明每个线程都有自己的栈的大小,jvm也有自己的大小,栈的宽度越大,创建的线程并行越小

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值