promotion image of download ymail app
Promoted
Anonymous
Anonymous asked in 電腦與網際網路程式設計 · 1 decade ago

寫程式高手請點進來!

作業系統老師,要我們寫程式,建立二個threads。

其中一個一直印出"How are you?"

另一個一直印出"I am fine."

我程式很弱...不會寫。

請高手幫幫我!T口T

(JAVA或C程式)

1 Answer

Rating
  • Lv 4
    1 decade ago
    Favorite Answer

    總共有三個class喔

    public class Thread1 implements Runnable{

    public void run() {

    while(true){

    try {

    Thread.sleep(1000);//故意睡個1秒~不然太快

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    System.out.println("How are you?");

    }

    }

    }

    ---------------------------------

    public class Thread2 implements Runnable{

    public void run() {

    while(true){

    try {

    Thread.sleep(1000);//故意睡個1秒~不然太快

    } catch (InterruptedException e) {

    e.printStackTrace();

    }

    System.out.println("I am fine.");

    }

    }

    }

    ----------------------------

    public class TestThread {

    public static void main(String arg[]){

    Runnable r1 = new Thread1();

    Runnable r2 = new Thread2();

    new Thread(r1).start();

    new Thread(r2).start();

    }

    }

    Source(s): me
    • Commenter avatarLogin to reply the answers
Still have questions? Get your answers by asking now.