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

CodePudding user response:

refer to the second floor left sichuan grain response:
add Thread in a for loop. Sleep (1000); Perform the following, let this thread every seconds, so you can see clearly the thread cross
why in cross editplus can see out, but can't see the idea

CodePudding user response:

And when I will be the main thread priority is set to the lowest, the child thread when set to the highest priority is still the same,,,

CodePudding user response:

Is the problem that the idea of

CodePudding user response:

My idea also appear such problem, has been the main output, Mr. Cheng added to 2000 to see cross, how do you solve

CodePudding user response:

reference 1st floor LSForever_ 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 the multi-core CPU run problem?


Threads run without control is totally random, discuss the meaningless, even if you set a priority level also can't guarantee high priority must first run,

CodePudding user response:

 public class Main {

The static List List=new CopyOnWriteArrayList<> (a);

Static class HelloThread extends Thread {
@ Override
Public void the run () {
For (int I=0; I & lt; 10000; I++) {
List. The add (1);
}
}
}

Public static void main (String [] args) throws InterruptedException {
HelloThread HelloThread=new HelloThread ();
HelloThread. Start ();
For (int I=0; I & lt; 10000; I++) {
List. Add (2);
}
HelloThread. The join ();
System. Out. Println (list);
}
}



Really meaningless to discuss
  • Related