After starting the thread, how can we save the running function? I have a lot of ideas, but I'm not sure if this is more professional?

In a Thread After calling start() , as you know, it will call the run() function in the Runnable class. In the run() function, I want the thread to remain as long as it receives a "Bye" message from the client. If I put them in a while loop, then I think this is a lot of memory, and I do not use the power of the stream. By the way, I do not want my thread to sleep in the start function by calling Thread.sleep(6000); Is there any other way to stay there in the launch function?

  • If the answer connects where and how and how to use it? Should I put it at the start of the launch function and it stays there until I send "Bye" from the client?

  • Should I say while((request=in.readLine())!=null){ ? This did not work, because I think that he will lose contact with the client or is it better to say that the client has lost the connection?

  • Should I say while(Thread.isAlive) and then kill the threat when I get "Bye" by calling Thread.stop , which is a little dangerous?

Here is my simplified code:

 while(true) { ClientWorker w; try { w = new ClientWorker(serverSocket.accept()); Thread t = new Thread(w); t.start(); } ...... } class ClientWorker implements Runnable { public ClientWorker(Socket incoming) { myList = my; this.incoming = incoming; } public synchronized void run() { in = new BufferedReader(new InputStreamReader(incoming.getInputStream())); . . .. ... } 
+7
source share
3 answers

In the simplest case, just let your run method do its job and complete it when appropriate, something like this:

 public void run() { boolean running = true; while(running) { running = doWork(); if (Thread.interrupted()) { return; } } } 

Here the process stops when the doWork () method returns false. It is also a good style to interrupt a stream, especially if it should work for a long time, see this guide . In order for this to work, doWork () must return regularly. Note that you cannot restart threads as soon as the run method returns a thread that will be restored by the system.

If you need more control over threads, you can create separate Worker and ThreadManager classes.

To allow ThreadManager to exit Worker, create a mutable logical field in your Worker that is periodically checked:

 public class Worker extends Thread { private volatile boolean running; public void run() { running = true; while(running) { running = doWork(); if (Thread.interrupted()) { return; } } } public void stopRunning() { running = false; } } 

The employee ends when he is interrupted or when the work is completed. ThreadManager can also request a Worker stop by calling the stopRunning () method.

If your thread runs out of work, it can also call the wait () method in ThreadManager. This pauses the flow until it is notified that there is a new job to do. ThreadManager should call notify () or notifyAll () when a new job arrives (in this example, ThreadManager is used as a monitor).

With this approach, you can keep the employee simple and interested only in the performance of work. ThreadManager determines the number of threads and makes the work available to them, but it does not need to know the details of the real work. Another step is to divide the β€œwork” into a separate class, which you can call β€œJob”. An example of this can be found in my example with a web scanner . This can be useful for transferring new work from ThreadManager to the workflow.

UPDATE: clarification: If a thread does not do any work and it just needs to wait for some condition, do not use iteration to wait, but use either wait / notify (see the web scanner example given by me) or a simple callback when the state arises.

+9
source

I know this is very late, but it is a much more efficient implementation and avoids heavy CPU usage by busy waiting. Allows you to switch context when a stream is waiting for a signal.

Setting the semaphore to 0 ensures that when a thread tries to get permission to continue, it does not receive it and does not wait. When you release the semaphore, the counter is incremented by 1, allowing the flow to continue, and the counter is reduced to 0.

Mostly:

 Semaphore sem = new Semaphore(0); Worker t = new Worker(sem); t.start();' // The thread is now running but waiting, signal the semaphore to start work sem.release(); 

Subject

 public class Worker(Semaphore sem) extends Thread { public void run() { sem.acquire(); doWork(); } } 
+4
source

just return from run() when you get "bye"

+2
source

All Articles