• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Starting a thread without calling from main

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a core java application running on a server. When I start the application from main, I want a another thread to be started automatically, without being explicitly called from main. It's like I need a Thread to be started when I start the JVM.

I want to put application monitoring code in that thread and make it into a jar file. By putting this jar file into the classpath of any application I want to be able to monitor them.

Is this possible???
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could create a thread in a static initialiser block, I guess.
 
Sandeep Sanaboyina
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,

Thanks for the reply.

I have tried that. I have written 2 classes MainClass.java and Monitor.java

The MainClass has my actual application and Monitor has my monitoring Code. Please find the code below.

None of the print statements in the Monitor.java are being printed.

MainClass.java

public class MainClass {
public static void main(String[] args) {
System.out.println("Running my application");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Monitor.java

public class Monitor implements Runnable {
public static Monitor mon = new Monitor();

public Monitor(){
System.out.println("Thread started from constructor");
}

static{
System.out.println("Thread started from static block");
}



public void run() {
// TODO Auto-generated method stub

}

}
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

None of the print statements in the Monitor.java are being printed.


Why would they? Class Monitor is never used, and thus never loaded or instantiated.

without being explicitly called from main


What do you mean by this? Any background thread you want to run will need to be created and started explicitly.
 
Sandeep Sanaboyina
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why would they? Class Monitor is never used, and thus never loaded or instantiated.


I know that Ulf , I was hoping this could be achieved by some roundabout coding.

What do you mean by this? Any background thread you want to run will need to be created and started explicitly.



I want to write a thread that can be put in a jar file and by keeping this jar in the classpath of any application, the thread should be started, when the JVM is launched. This is needed so that this jar can be used without changing any code in the actual application.
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code doesn't get executed unless something causes it to do so. If this were an JEE or servlet container, then there are ways of running code at server startup (servlet context initializers and @Startup beans).
 
Chris Hurst
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't look like what he wants is J2EE , and like you say unless he causes Monitor's class loader to be invoked the static initialiser won't get called. I presume there is a reason why you just can't change main and have your thread creation based on application parameter or invoke monitor and have a dummy implementation you could replace in if you didn't want thread creation ?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could load an 'agent' using java -javaagent option.
The agent code is executed after JVM initialises, but before the main is executed
- see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/package-summary.html
Using this you don't need to change the code of main, but this still isn't started automatically
- you must add -javaagent option to the invocation command of your class/jar.
 
Sandeep Sanaboyina
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Ireneusz..

This is exactly what I was looking for... It worked like a charm...

I am now able to create a JAR file and use it through out all my applications.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic