一个线程类,其run方法的不同实现?

时间:2016-12-17 14:51:12

标签: java multithreading

我有一个实现Thread的线程类。我想拥有这个类的3个实例,并且每个实例都有不同的线程类run方法的实现。我能这样做吗?我尝试将参数传递给run方法,但没有运气。这是我到目前为止尝试过的。

class thread extends Thread{
    int reelValue;
    JButton name;
    boolean stop;
    Reel r;
    int k;

    thread(int reelValue, JButton name, boolean stop, Reel r, int k){
        this.name = name;
        this.reelValue = reelValue;
        this.stop = stop;
        this.r = r;
        this.k = k;
    }
    public void run(){
        r = new Reel();
        while (!stop){
             k = ThreadLocalRandom.current().nextInt(0, 5 + 1);
            name.setIcon(r.symbols.get(k).getImage());
            reelValue = r.symbols.get(k).getValue();
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

public static void main(String[]args) {
    thread t1 = new thread(reelOneValue, reel1,stopT1, r1, a);
    thread t2 = new thread(reelTwoValue, reel2, stopT2, r2, b);
    thread t3 = new thread(reelThreeValue, reel3, stopT3, r3, c);

    t1.start();
    t2.start();
    t3.start();
}

0 个答案:

没有答案