Home > Back-end >  Java multi-thread problem, help bosses solutions, as shown in figure
Java multi-thread problem, help bosses solutions, as shown in figure

Time:09-16


Would you please tell me why, my program multithreading, data is added to the list, the results will be less,

CodePudding user response:

Prior to shutdown waitfinish.

CodePudding user response:

The child thread without end, the main line is the end of the first, so print Numbers, of course, will be less,

CodePudding user response:

refer to the original poster MAFT response:
could you please tell me, why my program multithreading, data is added to the list, the results will be less,,


 
Public static void main (String [] args) {
//TODO Auto - generated method stub
The ExecutorService service=Executors. NewFixedThreadPool (50);
For (int I=0; I<1000 * 10000; I++) {
Task Task=new Task (" a string: "+" "+ I);
Service. The execute (task);
}
Service. The shutdown ();
while(! Service. IsTerminated ()) {};
System. The out. Println (list. The size ());
}

CodePudding user response:

Upstairs bosses right

CodePudding user response:

Need waitfinish, or directly to close the thread pool, the task may not be performed

CodePudding user response:

Shutdown itself does not directly close the thread pool, will wait for the thread is complete before closing, shutdownnow will immediately close the thread pool, but the main the execution of the shutdown at the back of the print statements, thread pool thread is not over, so the result of the printing is not the final result,

CodePudding user response:

The application main thread of the building Lord before the end of the threads in the pool, so get is not the final result,

CodePudding user response:

You use the main method to test the main method is over the whole process stopped directly, either you is to let the main thread has been hold, every few seconds dormant, or you use springboot pull up a process

CodePudding user response:

refer to 6th floor qq_39936465 response:
shutdown itself does not directly close the thread pool, will wait for the thread is complete before closing, shutdownnow will immediately close the thread pool, but the main the execution of the shutdown at the back of the print statements, thread pool thread is not over, so the result of the printing is not the final result,

I think the 6th floor right

CodePudding user response:

The
refer to the original poster MAFT response:

Would you please tell me why, my program multithreading, data is added to the list, the results will be less,,


Program way of thinking will be a problem, the crux of the problem is that the original poster statement from the county seat, is that there is a queue,

Shutdown function is submitted will ensure each task can be processed with normal, but when you call shutdown function, the inside of the thread pool will not processing queue task,

Have you tried to create a thread pool objects, choose SynchronizedQueue to try, although there is no buffer function of the task, however, the data is consistent in the end,

, of course, this is not the best solution, this kind of scenario, generally use CountDownLatch, task in the countDown, the main thread using await wait for after the completion of all tasks are performed in the subsequent processing,

CodePudding user response:

 public static List List=Collections. SynchronizedList (new ArrayList<> ()); 

Private static class Task implements Runnable {
Private CountDownLatch CountDownLatch;
Private String String;

Public Task (CountDownLatch CountDownLatch, String String) {
Enclosing countDownLatch=countDownLatch;
This. String=string;
}

@ Override
Public void the run () {
Try {
List. Add (string);
} the finally {
CountDownLatch. CountDown ();
}
}
}

Public static void main (String [] args) {
Int count=1000 * 10000;
The ExecutorService pool=Executors. NewFixedThreadPool (10);
CountDownLatch CountDownLatch=new CountDownLatch (count);
for (int i=0; ITask Task=new Task (countDownLatch, a string: "\ t" + I);
Pool. Submit (task);
}

Try {
CountDownLatch. Await ();
{} catch InterruptedException (e)
e.printStackTrace();
}

The pool. The shutdown ();
System. The out. Println (list. The size ());
}

CodePudding user response:

 public static List List=Collections. SynchronizedList (new ArrayList<> ()); 

Private static class Task implements Runnable {
Private String String;

Public Task (String String) {
This. String=string;
}

@ Override
Public void the run () {
List. Add (string);
}
}

Public static void main (String [] args) {
Int count=1000 * 10000;
Int poolSize=10;
The ExecutorService pool=new ThreadPoolExecutor (poolSize, poolSize, 0 l, TimeUnit MILLISECONDS, new SynchronousQueue will (), new ThreadPoolExecutor. CallerRunsPolicy ());
for (int i=0; ITask Task=new Task (a string: "\ t" + I);
The pool. The execute (task);
}

The pool. The shutdown ();
System. The out. Println (list. The size ());
}

This code is not recommended
  • Related