Note that there are some explanatory texts on larger screens.

plurals
  1. POjava thread wait and auto wake up
    primarykey
    data
    text
    <p>I have an web application which accept some data from user to create a task, then the task should be executed. </p> <p>Since the execution of the Task is to download something from the internet which will cost some time, so I tried to create a new Thread to do the job.</p> <p>This is my idea:</p> <ol> <li><p>create a <code>LoaderThread</code> used to download data. And the <code>LoaderThread</code> hold a field of <code>ArrayList</code> used for put the <code>Task</code>.</p></li> <li><p>A <code>Servlet</code> to handle the request and response.</p></li> <li><p>When the <code>Servlet</code> startup, start the <code>LoaderThread</code></p></li> <li><p>During the servlet run, add task to the <code>LoaderThread</code>.</p></li> </ol> <p>This is the code(some of them is omitted):</p> <pre><code>public class RwdServlet extends HttpServlet { private StaticMapLoader loader; @Override public void init() throws ServletException { super.init(); loader = new StaticMapLoader(); loader.startRunning(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Task t=createTask(req); loader.addTask(t); } @Override public void destroy() { loader.stopRunning(); } } public class StaticMapLoader extends Thread { private List&lt;Task&gt; tasks = new ArrayList&lt;Task&gt;(); private boolean running = false; @Override public void run() { while (running) { if (tasks.size() &gt; 0) { Task t = tasks.get(0); log.info(t); if (t != null &amp;&amp; t.status == Status.waiting) { tasks.remove(0); t.status = Status.running; downLoad(t); } } } } private void downLoad(Task t) { //download file } public void addTask(Task t) { tasks.add(t); } public void startRunning() { running = true; this.start(); } public void stopRunning() { running = false; this.interrupt(); } } </code></pre> <p>The above code worked, but I found that even the <code>tasks</code> were empty and there are no new <code>task</code> added, the loop will keep running.</p> <p>So I though if I can make the <code>LoaderThread</code> suspend when there are no tasks, and notify it when new task come out.</p> <p>So I tried this:</p> <pre><code>@Override public void run() { while (running) { if (tasks.size() &gt; 0) { Task t = tasks.get(0); log.info(t); if (t != null &amp;&amp; t.status == Status.waiting) { tasks.remove(0); t.status = Status.running; downLoad(t); } } else { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } </code></pre> <p>I tried to call the <code>wait()</code> if the <code>tasks</code> is empty.</p> <p>But I do not know how to wake up it?</p> <p>Also, is there anything I should know to improve the application?</p> <p>BWT, is it possible that more than one <code>LoaderThread</code> instance will be created? If so , how to avoid it?</p> <hr> <p>It seems that I can use other implementation, but I wonder if my case is refactor-able?</p> <p>Since I want to learn some thing I have missed. :) Thanks. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload