Iplements a simple Threadpool that with dynanic thread count中文意思是实现一个简单的线程池的线程数与动态数据我希望是哪位高手能给我写一个程序,那我就非常感谢了!而且急呀!学了一个多月的Java还没进门,所以麻烦好心人给我谢谢程序吧!

解决方案 »

  1.   

    (以下程序使用内部类实现线程,对j增减的时候没有考虑顺序问题)
    public class ThreadTest1 {
    private int j;
    public static void main(String args[]) {
    ThreadTest1 tt = new ThreadTest1();
    Inc inc = tt.new Inc();
    Dec dec = tt.new Dec();
    for (int i = 0; i < 2; i++) {
    Thread t = new Thread(inc);
    t.start();
    t = new Thread(dec);
    t.start();
    }
    }
    private synchronized void inc() {
    j++;
    System.out.println(Thread.currentThread().getName() + “-inc:” + j);
    }
    private synchronized void dec() {
    j--;
    System.out.println(Thread.currentThread().getName() + “-dec:” + j);
    }class Inc implements Runnable {
    public void run() {
    for (int i = 0; i < 100; i++) {
    inc();
    }
    }
    }class Dec implements Runnable {
    public void run() {
    for (int i = 0; i < 100; i++) {
    dec();
    }
    }
    }
    }