1)写一个线程类class monitedThread。实例不做任何事情,只要求每个实例的运行时间是1~100s间的随机数;每个实例需要分配一个随机但不重复的id供主类区分;2)写主类Moniter,。A.必须是一个单体(Singleton)模式的类,即在一个应用下只有可能实例化一个对象B.他只允许最多3个monitedThread的实例同时存在;当发现一个线程消亡后,则产生一个新的线程,使系统内的线程数一直保持3个C.主类需探知(或线程通知主类,两种方式均可)线程的创建和消亡,i.每隔10秒打印一次目前系统下活着的所有线程的idii.有线程创建的时候主类打印“线程xxxxx创建,将运行xx秒钟”,消亡的时候主类打印“线程xxxx在运行xx秒后结束运行”

解决方案 »

  1.   

    这些东西要别人给你写 出来我想CSDN上没几个人愿意!!
    除飞你自己写的差不多了
    别人补下还是有可能的,。。,
      这事要靠自己!!
      

  2.   

    1、class monitedThread
    import java.util.Random;public class MonitedThread implements Runnable
    {    private int randomTime = 0;    private int id = 0;    public MonitedThread(int id)
        {        randomTime = new Random().nextInt(100);
            while (randomTime == 0)
            {
                randomTime = new Random().nextInt(100);
            }        this.id = id;    }    public String getThreadName()
        {
            return "Thread-" + id;
        }    /**
         * 
         */
        public void run()
        {
            System.out.println("线程[" + id + "]创建,将运行[" + randomTime + "]秒钟");
            try
            {
                Thread.sleep(randomTime * 1000);
            }
            catch (InterruptedException e)
            {
                // 捕获异常:异常处理
                e.printStackTrace();
            }
            System.out.println("线程[" + id + "]在运行[" + randomTime + "]秒后结束运行");
            Moniter.threadMap.remove(getThreadName());        // 通知需要创建线程
            Moniter.noteCreateThread();
        }}
      

  3.   

    2、Moniter
    import java.util.Collection;
    import java.util.concurrent.ConcurrentHashMap;public class Moniter
    {    private static Moniter moniter = new Moniter();    private static int maxMonitedThreadNum = 3;    public static ConcurrentHashMap<String, Thread> threadMap = new ConcurrentHashMap<String, Thread>(
                maxMonitedThreadNum);    // 私有化构造函数
        private Moniter()
        {    }    // 单实例
        public static Moniter getInstance()
        {
            if (null == moniter)
            {
                moniter = new Moniter();
            }
            return moniter;
        }    public void createThread()
        {
            // 创建3个monitedThread的实例
            for (int i = 0; i < maxMonitedThreadNum; i++)
            {
                MonitedThread rr = new MonitedThread(RandomDemo.getRandom());
                Thread t = new Thread(rr);
                t.setName(rr.getThreadName());
                threadMap.put("" + rr.getThreadName(), t);
                t.start();        }
        }    /**
         * 
         * @函数功能:[通知需要创建线程]
         */
        public static void noteCreateThread()
        {
            // 监控如果当前线程数少于最大数,就马上重新启动一个线程
            if (threadMap.size() < maxMonitedThreadNum)
            {
                MonitedThread rr = new MonitedThread(RandomDemo.getRandom());
                Thread t = new Thread(rr);
                t.setName(rr.getThreadName());
                threadMap.put("" + rr.getThreadName(), t);            t.start();
            }
        }    public void checkMonitor()
        {
            while (true)
            {
                Collection<Thread> theads = threadMap.values();
                System.out.println();
                System.out.println("=============================================");
                System.out.println("间隔10秒钟,当前的线程信息:");
                for (Thread t : theads)
                {
                    System.out.println(t.getName());
                }
                System.out.println("=============================================");
                System.out.println();
                try
                {
                    Thread.sleep(10 * 1000);
                }
                catch (InterruptedException e)
                {
                    // 捕获异常:异常处理
                    e.printStackTrace();
                }
            }
        }    public static void main(String[] args)
        {
            Moniter m = Moniter.getInstance();
            m.createThread();
            m.checkMonitor();
        }}3、随机不重复的数类
    import java.util.Random;/**
     * 
     * <产生数字不重复的随机数>
     * 
     */
    public class RandomDemo
    {
        private static int num[] = new int[10000];    static
        {
            for (int i = 0; i < num.length; i++)
            {
                num[i] = i;
            }
        }    public static int getRandom()
        {
            Random rnd = new Random();
            int tmp = Math.abs(rnd.nextInt()) % num.length;        int retNum = 0;
            if (num[tmp] != -1) // 通过这个控制不重复
            {
                retNum = num[tmp];
                System.out.println(tmp);
                num[tmp] = -1; // 通过这个控制不重复
            }        return retNum;
        }}