Showing posts with label Thread. Show all posts
Showing posts with label Thread. Show all posts

Java: concurrency with Runnable

In this simple tutorial we will see how to process some work in multiple threads using limited pool of x threads. Please note that there is an overhead in using threads.

You could use number of available CPUs as your pool size.



Result:


CPUs 8
thread with value 1 current sum 3
thread with value 2 current sum 3
thread with value 3 current sum 6
thread with value 4 current sum 10
thread with value 5 current sum 15
thread with value 6 current sum 21
thread with value 7 current sum 28
thread with value 8 current sum 36
final sum 120
thread with value 11 current sum 66
thread with value 13 current sum 91
thread with value 15 current sum 120
thread with value 9 current sum 45
thread with value 14 current sum 105
thread with value 12 current sum 78
thread with value 10 current sum 55



As an Amazon Associate I earn from qualifying purchases.

Java: concurrency with Runnable

In this simple tutorial we will see how to process some work in multiple threads using limited pool of x threads. Please note that there is an overhead in using threads.

You could use number of available CPUs as your pool size.



Result:


CPUs 8
thread with value 1 current sum 3
thread with value 2 current sum 3
thread with value 3 current sum 6
thread with value 4 current sum 10
thread with value 5 current sum 15
thread with value 6 current sum 21
thread with value 7 current sum 28
thread with value 8 current sum 36
final sum 120
thread with value 11 current sum 66
thread with value 13 current sum 91
thread with value 15 current sum 120
thread with value 9 current sum 45
thread with value 14 current sum 105
thread with value 12 current sum 78
thread with value 10 current sum 55



As an Amazon Associate I earn from qualifying purchases.

Java Servlet: starting a thread that always runs

  • when Java Servlet starts it reads web.xml
  • add listener implementation class to your web.xml



<display-name>your_servlet_namedisplay-name>

<listener>
<listener-class>com.your_package_name.TimedServletCallerlistener-class>
listener>

  • we will use ServletContextListener interface
  • create a NEW thread (Loop) inside contextInitialized(), if you did Thread.sleep without new thread the whole Servlet would pause and container would fail to start it after 45 seconds or so


public class TimedServletCaller implements ServletContextListener
{
private static int sleepMinutes = 1;

class Loop extends Thread
{
public void run()
{
while (true)
{
Log.e("Loop is running!");
takeShortNap();
// do stuff here
}
}
}

@Override
public void contextInitialized(ServletContextEvent arg0)
{
Log.e("***** TimedServletCaller.contextInitialized()");
// execute();
Thread thread = new Loop();
thread.start();
}

private static void takeShortNap()
{
Log.i(" Pausing for " + sleepMinutes + " minute(s).");
try
{
Thread.sleep(sleepMinutes * 60 * 1000);
catch (InterruptedException e)
{
Log.e(e.getMessage());
}
}

  • restart your server (Tomcat) now you can use your Servlet, but also the Loop keeps running and doing useful things like database updates, etc.


As an Amazon Associate I earn from qualifying purchases.

Java Servlet: starting a thread that always runs

  • when Java Servlet starts it reads web.xml
  • add listener implementation class to your web.xml



<display-name>your_servlet_namedisplay-name>

<listener>
<listener-class>com.your_package_name.TimedServletCallerlistener-class>
listener>

  • we will use ServletContextListener interface
  • create a NEW thread (Loop) inside contextInitialized(), if you did Thread.sleep without new thread the whole Servlet would pause and container would fail to start it after 45 seconds or so


public class TimedServletCaller implements ServletContextListener
{
private static int sleepMinutes = 1;

class Loop extends Thread
{
public void run()
{
while (true)
{
Log.e("Loop is running!");
takeShortNap();
// do stuff here
}
}
}

@Override
public void contextInitialized(ServletContextEvent arg0)
{
Log.e("***** TimedServletCaller.contextInitialized()");
// execute();
Thread thread = new Loop();
thread.start();
}

private static void takeShortNap()
{
Log.i(" Pausing for " + sleepMinutes + " minute(s).");
try
{
Thread.sleep(sleepMinutes * 60 * 1000);
catch (InterruptedException e)
{
Log.e(e.getMessage());
}
}

  • restart your server (Tomcat) now you can use your Servlet, but also the Loop keeps running and doing useful things like database updates, etc.


As an Amazon Associate I earn from qualifying purchases.

apt quotation..