簡體   English   中英

如何創建和運行新線程

[英]How to create and run a new thread

我需要有人幫助我檢查代碼,並提供解決方案以獲取輸出。 這是我的編碼:

class MyThread extends Thread {

    private String name, msg;

    public MyThread(String name) {
        this.name = name;

    }

    public void run() {

        System.out.println("Child Thread : starts its execution");

        for (int i = 5 ; i > 0 ; i--) {

            System.out.println(name + " says: " + i);
            try {
                Thread.sleep(2000);
            }
            catch (InterruptedException ie) {
            }
        }

        System.out.println(name + " finished execution");
    }

    public static void main(String [] args) {
        MyThread mt1 = new MyThread("Main Thread");
        MyThread mt2 = new MyThread("Child Thread");

        mt1.start();
        mt2.start();

    }
}

輸出應如下所示:

在此處輸入圖片說明

但是我的編碼無法正常工作(邏輯錯誤)。 任何人都可以幫助我獲得如上所述的輸出嗎?

嘗試這個。 我已經發表了意見。 隨時提出任何疑問

public class MyThread extends Thread {
    private String name, msg;
    private int interval; // to specify the period to be used to take lock. Main thread needs to do it for every iteration whereas childthread should do it for 2 iterations
    private static Object lock = new Object(); // to use wait and notify mechanism to get the desired output
    private static boolean done = false; // to make sure that if the child thread is done with its tasks then main thread need not wait

    public MyThread(String name, int interval) {
        this.name = name; 
        this.interval = interval;
    }

    public void run() {
        int count = 0;
        synchronized (lock) {

            System.out.println(name+" Thread : starts its execution");

            for (int i = 5; i > 0; i--) {

                System.out.println(name + " says: " +i);
                try {
                    Thread.sleep(200);
                } catch (InterruptedException ie) {}

                // Main thread waits for every iteration
                // Child thread waits for every 2 iterations
                if(!done && ++count % interval == 0) {
                    lock.notifyAll();
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            // Once child thread is done with its iterations, main thread need not wait anymore hence set done to true
            done = true;
            // to make sure that if the main thread is waiting then give it a signal to finish its tasks with no more waiting
            lock.notifyAll();
            System.out.println(name + " finished execution");
        }
    }

    public static void main(String[] args) {
        MyThread mt1 = new MyThread("Main Thread",1);
        MyThread mt2 = new MyThread("Child Thread",2);

        mt1.start();
        mt2.start();

    }

}

暫無
暫無

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

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