In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
The main content of this article is to explain "what is the Worker Thread mode of Java multithreading". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the Worker Thread mode of Java multithreading"?
I. Worker Thread mode
Worker means the person who works. In Worker Thread mode, the worker thread Worker thread will retrieve the work one by one and process it. When all the work is done, the worker thread will wait for the new work to arrive.
The Worker Thread pattern is also known as the Background Thread (background Thread) mode, and we can also call it the Thread Pool pattern from the point of view of the place where multiple worker threads are saved.
II. Role 1.Client (delegate) in worker Thread mode
Create a Request that represents a work request and pass it to Channel. In the sample program, ClientThread is equivalent to this role.
2.Channel (communication line)
The Channel role accepts the Request from Client and passes it to Worker. In the sample program, Channel is equivalent to this role.
3.Worker (worker)
The Worker role takes the Request from the Channel and works, and when one work is done, it continues to get another Request, and in the sample program, WorkerThread is equivalent to that role.
4.Request (request)
The Request role is the role that represents the work, and the Request role holds the information necessary to do the work, and in the sample program, Request is equivalent to that role.
III. Worker Thread usage scenarios
Imagine a scene in which a factory produces toys. In a workshop, there are several workers. Every time the production parts are ready, the people outside the workshop put the parts on a table in the workshop, and the workers take the parts from the table every time they finish making a toy. Here, note that the parts are not handed over directly to the workers, and another point is that the workers do not finish a part and go home for a new person, which is a bit funny in reality, but corresponds to a typical thread usage in the program: thread pool.
The so-called thread pool is the reuse of threads, which continues to execute other tasks after the thread has finished executing tasks, rather than destroying and starting new threads to execute other tasks. Because the startup of threads is very expensive for system performance, it is very good for the improvement of system performance.
IV. Worker Thread mode program example
The first is the request, that is, the parts of the toy.
Public class Request {private final String name; private final int number; public Request (String name, int number) {this.name = name; this.number = number;} public void execute () {System.out.println (Thread.currentThread (). GetName () + "executed" + this);} @ Override public String toString () {return "Request= >" + "No." + number + "Name." + name;}}
This is a simple class that prints out fields when you have name and number and execute.
ClientThread, which is responsible for putting the request into the RequestQueue, that is, putting the parts on the table.
Public class ClientThread extends Thread {private static final Random random = new Random (System.currentTimeMillis ()); private final Channel channel; public ClientThread (String name, Channel channel) {super (name); this.channel = channel;} @ Override public void run () {try {for (int I = 0; true; iTunes +) {Request request = new Request (getName (), I) This.channel.put (request); Thread.sleep (random.nextInt (1000000));} catch (Exception e) {}
Channel class, which can be used as a workshop
Public class Channel {private final static int MAX_REQUEST = 100; private final Request [] requestQueue; private final WorkerThread [] workerPool; private int head; private int tail; private int count; public Channel (int workers) {this.requestQueue = new request [Max _ REQUEST]; this.head = 0; this.tail = 0; this.count = 0; this.workerPool = new WorkerThread [workers]; this.init () } private void init () {for (int I = 0; I)
< workerPool.length; i++) { workerPool[i] = new WorkerThread("Worker-" + i, this); } } /** * push switch to start all of worker to work */ public void startWorker() { Arrays.asList(workerPool).forEach(WorkerThread::start);// List workerThreads = Arrays.asList(workerPool);//// workerThreads.stream().forEach(WorkerThread::start); } public synchronized void put(Request request) { while (count >= requestQueue.length) {try {this.wait ();} catch (InterruptedException e) {e.printStackTrace ();}} this.requestQueue [tail] = request; this.tail = (tail + 1)% requestQueue.length; this.count++; this.notifyAll () } public synchronized Request take () {while (count No.0 Name.Alex)
Worker-2 executed Request= > No.0 Name.Jack
Worker-3 executed Request= > No.0 Name.William
Worker-4 executed Request= > No.1 Name.Jack
Worker-0 executed Request= > No.1 Name.William
Worker-3 executed Request= > No.2 Name.Jack
Worker-2 executed Request= > No.1 Name.Alex
Worker-4 executed Request= > No.2 Name.William
Worker-1 executed Request= > No.3 Name.Jack
Worker-3 executed Request= > No.2 Name.Alex
Worker-4 executed Request= > No.3 Name.William
Worker-0 executed Request= > No.4 Name.Jack
Worker-0 executed Request= > No.3 Name.Alex
Worker-1 executed Request= > No.5 Name.Jack
Worker-3 executed Request= > No.4 Name.William
Worker-1 executed Request= > No.6 Name.Jack
Worker-2 executed Request= > No.4 Name.Alex
Worker-3 executed Request= > No.7 Name.Jack
Worker-0 executed Request= > No.5 Name.William
Worker-1 executed Request= > No.5 Name.Alex
Worker-4 executed Request= > No.8 Name.Jack
Worker-2 executed Request= > No.6 Name.Alex
Worker-0 executed Request= > No.7 Name.Alex
Worker-4 executed Request= > No.8 Name.Alex
Worker-2 executed Request= > No.6 Name.William
Omit.
You can see that the threads that perform tasks are the five threads of WorkerThread1,2,3,4,5 that are constantly performing request tasks from ClientThread Alex,Jack,William.
At this point, I believe that everyone on the "Java multithreading what is Worker Thread mode" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 267
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.