Home > Back-end > The idea of multi-thread problem, be badly in need of a great god solve!!!!!!
The idea of multi-thread problem, be badly in need of a great god solve!!!!!!
Time:10-22
When learning a multithreaded found problems When using the idea of I look not to come out among the main thread, and the child thread cross No matter how many times running results are main line first, and then the child thread I compile program to test editplus run found no problem, there will be a cross between two threads Is no idea,, Hope to have a great god can answer!!!!!
Below is the procedure:
/* * * @ auther Zebra * @ the create the 2020-02-02-0:48 */ The class HelloThread extends Thread { @ Override Public void the run () { for(int i=0; I & lt; 100; I++) If (I % 2==0) { System. The out. Println (Thread. CurrentThread (). The getName () + ":" + I);
}
} Public HelloThread (String name) { Super (name); } } Public class ThreadMethodTest { Public static void main (String [] args) { HelloThread h1=new HelloThread (" Thread ");
//h1. Elegantly-named setName (" thread ");
H1. The start ();
//to the main thread named Thread. CurrentThread (). Elegantly-named setName (" main Thread ");
for(int i=0; I & lt; 100; I++) If (I % 2==0) { System. The out. Println (Thread. CurrentThread (). The getName () + ":" + I);
}
}
CodePudding user response:
After tests found that when the main thread of the cycle times are set to 5000, began to have the cross, but the thread is about to finish the, because multi-core CPU run problem?
CodePudding user response:
Add the Thread in the for loop. Sleep (1000); , let this thread every one second, perform the following cross so you can see clear thread