Home > Back-end >  Ask questions about blocking queue ThreadPoolExecutor as well
Ask questions about blocking queue ThreadPoolExecutor as well

Time:09-21

Few words said, the first code:
 public class BIOTest {
Public static void main (String [] args) throws IOException {
The ExecutorService eService=new ThreadPoolExecutor (
1,
The Runtime. GetRuntime (). AvailableProcessors (),
5,
TimeUnit. SECONDS,
New LinkedBlockingQueue (3),
Executors. DefaultThreadFactory (),
New ThreadPoolExecutor. AbortPolicy ()
);
ServerSocket sSocket=new ServerSocket (9527);
System. The out. Println (" service start ");

While (true) {
//listen
Final Socket Socket=sSocket. The accept ();
System. The out. Println (" someone come in ");

EService. Execute (() - & gt; {
Handler (socket);
});
}


}

Public static void handler (Socket Socket) {

Try {
//System. Out. Println (" is the current Thread is: "+ Thread. CurrentThread (). The getName ());
Byte [] bytes=new byte [1024].
InputStream inp=socket. GetInputStream ();
While (true) {
Int read=inp. Read (bytes);
if(read!=1) {
System. The out. Println (Thread. CurrentThread (). The getName () + ":" + new String (bytes, 0, read));
} else {break; }
}
} the catch (Exception e) {
//TODO: handle the exception
e.printStackTrace();
} the finally {
System. Out. Println (Thread. CurrentThread (). The getName () + "out");
Try {
Socket. The close ();
{} catch Exception (e2)
//TODO: handle the exception
E2. PrintStackTrace ();
}
}
}
}

Now the problem is, when I use the Linked/ArrayBlockingQueue, using the Telnet command 127.0.0.1 9527 has been only one thread, two Windows running this command will form the jam, must be closed first window to make the second window to send the information to send to the server, but using SynchronousQueue will, there is no this problem, why?

CodePudding user response:

I went to, how to modify my original meaning, not interrupt, too is a second window to send message to the console output, must be closed first window, the second window to send information to output, the most main is don't know why there is only one thread but using SynchronousQueue will, there is no this problem

CodePudding user response:

Release resources, SynchronousQueue will this should automatically release resources, other need to manually release

CodePudding user response:

There is nothing strange questions, appearing only no research problems,
Change the core thread pool threads try,

CodePudding user response:

The thread pool execution order core thread - & gt; Waiting queue - & gt; Maximum thread, you 1 core number of threads, and use is LinkedBlockingQueue, of course, only one thread can normal operation, another thread tasks are exist in the queue, will not open a new thread to execute, and SynchronousQueue will this queue is special blocking queue, it actually not stored internally task, you can go to see the characteristics of SynchronousQueue will

CodePudding user response:

references a handsome, 4/f, forced response:
thread pool execution order core thread - & gt; Waiting queue - & gt; Maximum thread, you 1 core number of threads, and use is LinkedBlockingQueue, of course, only one thread can normal operation, another thread tasks are exist in the queue, will not open a new thread to execute, and SynchronousQueue will this queue is special blocking queue, it internal actual not storage tasks, you can go to look at the characteristics of SynchronousQueue will

This I know, I see posts to asking questions, post is this: https://blog.csdn.net/qq_26881739/article/details/80983495
I was just wondering, with a little bit about the near code is the effect of the business, is a for loop looks before executing thread pool is not the problem

CodePudding user response:

refer to the second floor small yards life reply:
release resources, SynchronousQueue will this should automatically release resources, other need to manually release

I remember there is a similar. The close () method can be invoked, using the release?

CodePudding user response:

Thread pool capacity only 1, you use SynchronousQueue will, unless you are completely the same two window time slice in the message, otherwise is certainly a thread that use SynchronousQueue will queue and then withdrew, and then the second window with the inside of the pool thread consumption SynchronousQueue will queue again, of course not blocked, I don't believe you if it's the same timestamp in hair message, can use SynchronousQueue will also parallel
You can write a client to simulate, sleep under the thread don't immediately quit, try SynchronousQueue will

And what do you want to achieve the business scenario? If you want to thread of precise logic and concurrent control granularity, suggest you use concurrent package under all kinds of locks, and don't rely on the inside of the ExecutorThread queue containers

CodePudding user response:

reference 5 floor JSP_killmylife reply:
Quote: refer to 4th floor, a handsome and forced response:

The thread pool execution order core thread - & gt; Waiting queue - & gt; Maximum thread, you 1 core number of threads, and use is LinkedBlockingQueue, of course, only one thread can normal operation, another thread tasks are exist in the queue, will not open a new thread to execute, and SynchronousQueue will this queue is special blocking queue, it internal actual not storage tasks, you can go to look at the characteristics of SynchronousQueue will

This I know, I see posts to asking questions, post is this: https://blog.csdn.net/qq_26881739/article/details/80983495
I was just wondering, with a little bit about the near code is the effect of the business, is a for loop looks before executing thread pool is not the problem of
what you doubt that, what is your problem, you watched the post, is simply introduced the three queue, you can try to make your statement concise point

CodePudding user response:

references a handsome eighth floor forced response:
Quote: refer to the fifth floor JSP_killmylife reply:

Quote: refer to 4th floor, a handsome and forced response:

The thread pool execution order core thread - & gt; Waiting queue - & gt; Maximum thread, you 1 core number of threads, and use is LinkedBlockingQueue, of course, only one thread can normal operation, another thread tasks are exist in the queue, will not open a new thread to execute, and SynchronousQueue will this queue is special blocking queue, it internal actual not storage tasks, you can go to look at the characteristics of SynchronousQueue will

This I know, I see posts to asking questions, post is this: https://blog.csdn.net/qq_26881739/article/details/80983495
I was just wondering, with a little bit about the near code is the effect of the business, is a for loop looks before executing thread pool is not the problem of
what you doubt that, what is your problem, you watched the post, is simply introduced the three queue, you can try to make your statement concise point


My my, don't want to want to ask, I just want to know why there is only one core thread work, clearly identify the largest core thread

CodePudding user response:

refer to 7th floor qingyuan18 response:
thread pool capacity only 1, you use SynchronousQueue will, unless you are completely the same two window time slice in the message, otherwise certainly is a thread consumption SynchronousQueue will queue and then withdrew, and then the second window with the inside of the pool thread consumption SynchronousQueue will queue again, of course not blocked, I don't believe you if it's the same timestamp in hair message, can use SynchronousQueue will also parallel
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related