Ich habe versucht, ein Programm zu schreiben, das Threads verwendet, aber das O/P nicht verstehen konnte. Ich habe 2 Gewinde: s
und t
. Aber ich kann nicht sehen, dass der Thread s
funktioniert.Thread Behavior: Java Anfänger
Kann mir bitte jemand das Verhalten erklären? Dank
Mein Code:
public class Bground implements Runnable
{
Thread t,s;
Bground()
{
t = new Thread(this,"first thread");
s = new Thread(this,"second thread");
s.start();
t.start();
}
public void run()
{
System.out.println("inside run" + t.getName());
try
{
for (int i = 0; i < 5; i++)
{
System.out.println("In child thread" + i);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String argv[])
{
Bground b = new Bground();
try
{
for (int i = 0; i < 5; i++)
{
System.out.println("In main thread" + i);
Thread.sleep(1);
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
O/P:
c: \ Programme \ Java \ jdk1.6.0_23 \ bin> java Bground
In Haupt Thread0
innerhalb runfirst thread
innerhalb runfirst thread
In untergeordnete thread0
In Kind thread1
In Kind thread2
Im Haupt thread1
In Kind thread3
In Kind Thread0
In Kind thread1
In Kind thread2
In Kind thread3
In Kind thread4
In Haupt-thread2
In untergeordnete thread4
In main thread3
In main thread4
Nur um Sie wissen zu lassen: Jedes Mal, wenn Sie Ihren Code so formatieren, tötet Gott ein Kätzchen. –
Überlegen Sie, ob jeder Thread ein 'Thread.sleep (1000)' hat. Das Argument ist in Millisekunden. –