Public static void main (String [] args) {
Thread thread01=new Thread (() - & gt; {
for (int i=0; I & lt; 10; + + I) {
System. Out.println (Thread. CurrentThread (). The getName () + "... I="+ I);
}
}, "thread01");
Thread01. Start ();
for (int i=0; I & lt; 10; + + I) {
System. Out.println (Thread. CurrentThread (). The getName () + "... J="+ I);
}
Recently in multithreaded. Perform a code found on a problem: thread01 may not print 10 times. Ask the reason? Is the main thread will affect the child thread
CodePudding user response:
The main... J=0
The main... J=1
The main... J=2
The main... J=3
The main... J=4
The main... J=5
The main... J=6
The main... J=7
The main... J=8
The main... J=9
Thread01... I=0
Thread01... I=1
Thread01... I=2
Thread01... I=3
The results
CodePudding user response:
Why didn't you this multithreaded run () method to run the
CodePudding user response:
refer to the second floor at any time in the efforts were not late reply: why didn't you this multithreaded run () method to run the is not only a thread? CodePudding user response:
In addition to the main thread CodePudding user response:
reference 1st floor weixin_43456806 response: main... J=0 The main... J=1 The main... J=2 The main... J=3 The main... J=4 The main... J=5 The main... J=6 The main... J=7 The main... J=8 The main... J=9 Thread01... I=0 Thread01... I=1 Thread01... I=2 Thread01... I=3 Run results main thread ran out, the end of the program, certainly can't see, use thread01. The join () is waiting to be performed CodePudding user response:
You this should be a misunderstanding caused by the IDE, there is a delay to be output to the console, procedure is output to the console, IDE control console is at the end of the program immediately shut down, may be could display program is out of the caused this illusion, You can add a delay to the thread 1, look at: Thread thread01=new Thread (() - & gt; { for (int i=0; I & lt; 10; + + I) { Try { TimeUnit. SECONDS. Sleep (1); {} catch InterruptedException (e) e.printStackTrace(); } System. Out.println (Thread. CurrentThread (). The getName () + "... I="+ I); } }, "thread01"); Thread01. Start (); for (int i=0; I & lt; 10; + + I) { System. Out.println (Thread. CurrentThread (). The getName () + "... J="+ I); } CodePudding user response:
reference 1st floor weixin_43456806 response: run results Program should be no problem, but you run the result is problematic, I tested it is normal to the end of September, the results don't know how you, CodePudding user response:
Refer to the fifth floor said Ask the baidu keyword: daemon thread, you can also ask the join CodePudding user response:
Created by the original poster is not a daemon thread, is not end with the end of the main thread, My idea or to run your code with CMD will not have a problem, estimates that really is the cause of the 6th floor says, there is something wrong with your idea CodePudding user response:
I suggest change the computer here CodePudding user response:
Thread0 enough priority bai, the main thread is running out, didn't end the child thread, however, the main thread end also forced child thread exit together, you can add a lock, and wait on the main thread (); Public static void main (String [] args) { The Object locker=new Object (); Thread thread01=new Thread (() - & gt; { Synchronized (locker) { for (int i=0; I & lt; 10; + + I) { System. Out.println (Thread. CurrentThread (). The getName () + "... I="+ I); } Locker. Notify (); } }, "thread01"); Thread01. Start (); for (int i=0; I & lt; 10; + + I) { System. Out.println (Thread. CurrentThread (). The getName () + "... J="+ I); } Try { Synchronized (locker) { Locker. Wait (); } {} catch InterruptedException (e) //TODO Auto - generated the catch block, e.printStackTrace(); } } CodePudding user response:
Traverse to the child Thread is set to the daemon Thread will appear the original poster said, is that the Thread has a pair of non-static methods Boolean isDaemon ()/setDaemon (Boolean) CodePudding user response:
May be ide console print problem, you are in the main thread after the child thread start sleep for a while and then look at the end of the result CodePudding user response:
The main thread is dead, thread01 will perish, and the solution? (1) to the main thread waits for (2) in order to protect the thread run So you can print out the results you want, CodePudding user response:
Idea to print the results might be the problem, use your code can be run on eclipse complete thread01 0-9 CodePudding user response:
The operation of the main thread will affect the child thread, the child thread depends on the main thread running, when the main thread ends, and the child thread ends, the solution: (1) for the main thread to wait, make the child thread run over, (2) sets the child thread to daemon thread,