簡體   English   中英

兩個線程不同的run方法和並發工作

[英]Two thread diffrent run method and work concurrently

我想同時運行兩個不同的run方法的線程。 可能嗎? 我在下面有簡單的代碼,但它們並不兼容。 我只想每隔5秒運行第一個線程,第二個線程總是運行。

public static int x = 0;

public static void main(String[] args) throws InterruptedException{

    Runnable r1 = new Runnable() {
        public void run() {
            x = x + 1;
            System.out.println("increment x");
        }
    };

    Runnable r2 = new Runnable() {
        public void run() {
            System.out.println("x is "+x);
        }
    };

    while(true){
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();
        t1.sleep(5000);
    }
}

你的run()方法只增加x,打印並返回。 一旦run方法返回,線程就會停止運行。 如果你想要永遠運行某些東西,你需要在run()方法中有一個循環。

而且,在沒有任何同步的情況下訪問共享變量將不會產生可預測的結果。 你的x變量至少應該是不穩定的。 最后,Thread.sleep()是一個靜態方法。 因此應該使用類名調用它:

Thread.sleep(5000L);

它使當前線程休眠。

下面是一個示例,其中一個線程每500毫秒遞增x,另一個線程每100毫秒打印x,並且兩個線程在5秒后中斷:

public class ThreadExample {
    private static volatile int x;

    private static class Incrementer implements Runnable {
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                x++;
                try {
                    Thread.sleep(500L);
                }
                catch (InterruptedException e) {
                    return;
                }
            }
        }
    }

    private static class Reader implements Runnable {
        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println(x);
                try {
                    Thread.sleep(100L);
                }
                catch (InterruptedException e) {
                    return;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Incrementer());
        Thread t2 = new Thread(new Reader());
        t1.start();
        t2.start();

        try {
            Thread.sleep(5000L);
        }
        finally {
            t1.interrupt();
            t2.interrupt();
        }
    }
}

在while循環中,您每次創建新的Threads然后start它們,這就是您有這種行為的原因。

然后你需要在線程中移動while循環,這樣它們就像下面一樣:

public static volatile int x = 0;

public static void main(String[] args) throws InterruptedException{

Runnable r1 = new Runnable() {
    public void run() {
      while (true) {
        x = x + 1;
        System.out.println("increment x");
        this.sleep(5000);
      }
    }
};

Runnable r2 = new Runnable() {
    public void run() {
      while(true){
        System.out.println("x is "+x);
      }
    }
};
 Thread t1 = new Thread(r1);
 Thread t2 = new Thread(r2);
 t1.start();
 t2.start();
}

另請注意,應同步訪問x變量以查看正確的值。

BR。

    Thread t1;;
    Thread t2; 
while(true){
      t1 = new Thread(r1);
      t2= new Thread(r2);
      t1.start();
      t1.sleep(5000);
      t2.start();

}

你正在添加代碼是正確的,在t2.start()之前將代碼放到t1.sleep(5000)。

public class Test {
public static int x = 0;

public static void main(String[] args) throws InterruptedException{

    Runnable r1 = new Runnable() {
        public void run() {
            while(true){
            x = x + 1;
            System.out.println("increment x");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
        }
    };

    Runnable r2 = new Runnable() {
        public void run() {
            while(true){
            System.out.println("x is "+x);
            }
        }
    };
        Thread t1 = new Thread(r1);
        Thread t2 = new Thread(r2);
        t1.start();
        t2.start();

// Thread.sleep(5000);

}

}

像這樣的東西..但請記住上面提到的觀點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM