搜尋此網誌

2012年5月7日 星期一

Thread Sleep sample , 執行緒 sleep範例.

package thread;

public class ThreadOperation {

    public static void main(String[] args) {

        Thread t1 = new Thread(new Th1());
        Thread t2 = new Thread(new Th2());
        t1.start();
        t2.start();
    }

}

class Th1 implements Runnable {

    public void run() {
        while (true) {
            System.out.println("Th1 ...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}

class Th2 implements Runnable {

    public void run() {
        while (true) {
            System.out.println("Th2 ...");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}

沒有留言:

張貼留言