Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I initialize a static member once and share it across threads?
    primarykey
    data
    text
    <p>This is my first time attempting to write a program that makes use of multithreading, so I have several questions regarding the use of concurrency in my program.</p> <p>My program takes user input from a web UI, and then starts a process with that user input. I know I have to make use of concurrency because this process takes upwards of an hour, and I cannot possibly have the user wait for one process to complete before starting the next.</p> <p>The following simplified code handles the user input and then starts the process.</p> <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String myInput = request.getParameter("input"); Thread t = new Thread(new MyRunnable(myInput)); t.start(); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("Process started!"); out.close(); } </code></pre> <p>The following code is a simplification of my actual process.</p> <pre><code>public class MyRunnable implements Runnable { private static HashMap&lt;String,String&gt; mapOfConstants = null; private String member; public MyRunnable(String member) { this.member = member; } @Override public void run() { if (mapOfConstants == null) init(); // and so on... } private void init() { mapOfConstants = new HashMap&lt;String,String&gt;(); mapOfConstants.put("LOCATION", "http://localhost/folder"); // and so on... } } </code></pre> <p>In my code above, I intend to define a series of placeholders as a constant, which will be stored in the HashMap <code>mapOfConstants</code>.</p> <p>EDIT: Eventually I might want to make it such that initialization of this map take values from elsewhere, say a text file.</p> <p>Does my code achieve the purpose of sharing this placeholder map across all instances of <code>MyRunnable</code>, doing this initialization process only once?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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