Loading

多个线程作用于同一个runnable对象

多个线程作用于同一个runnable对象

学习了:https://www.cnblogs.com/ligang305/archive/2012/08/10/2632126.html

http://ask.bdqn.cn/view/15700

http://blog.csdn.net/tomli2017/article/details/70176732

虽然Runnable是一个,但是线程是十个;

package com.stono.thread2;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.TimeUnit;

public class Runnable2Threads implements Runnable {
    private int i;
    
    public Runnable2Threads(int i) {
        this.i = i;
    }

    public static void main(String[] args) {
        Runnable2Threads runnable2Threads = new Runnable2Threads(100);
        for(int i=0;i<10;i++){
            Thread thread = new Thread(runnable2Threads);
            thread.start();
        }
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        ThreadInfo[] dumpAllThreads = threadMXBean.dumpAllThreads(false, false);
        for (ThreadInfo threadInfo : dumpAllThreads) {
            // 虽然runnable是一个,但是线程却是十个;
            System.out.println(threadInfo.getThreadName());
        }
    }

    @Override
    public void run() {
        System.out.println(i);
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

 

posted @ 2018-01-28 09:31  stono  阅读(275)  评论(0编辑  收藏  举报