0
我想創建3個子線程和 父線程必須顯示1-5個數字, 子1必須顯示6-10, 子2必須顯示11-15, 孩子3必須顯示15-20 ...多線程在同一類使用一個循環,並使每個線程顯示
並且這必須使用一個for循環在父類中,一個for循環在子類中。 C類是父類和類D是孩子
public class C {
public static void main(String[] args){
D d = new D();
System.out.println(Thread.currentThread().getName());
Thread child1= new Thread(d);
Thread child2= new Thread(d);
Thread child3= new Thread(d);
child1.start();
child2.start();
child3.start();
for(int i=1; i<=5;i++){
System.out.println("From parent thread: " +i);
}
}
public class D extends Thread{
public void run(){
System.out.println(Thread.currentThread().getName());
}
好的,你已經描述了你想要做的。現在你需要編輯你的問題並添加一個問題。你有什麼具體的編程難度?包括輸入,預期與實際輸出,錯誤等。 – azurefrog
我相信它是一個家庭任務或其他東西。 –
@AmitK.Saha好的,這是相當明顯的,但作爲家庭作業的任務並不會造成本質上不好的問題或脫離主題的問題(儘管這個問題需要一些工作)。 – azurefrog