aspose file tools*
The moose likes Threads and Synchronization and the fly likes Starting a thread without calling from main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Starting a thread without calling from main" Watch "Starting a thread without calling from main" New topic
Author

Starting a thread without calling from main

Sandeep Sanaboyina
Ranch Hand

Joined: Dec 14, 2009
Posts: 72
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???


They say you have to be the first, the best or different. I say, is it too much to ask for all three.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 418
    
    2

You could create a thread in a static initialiser block, I guess.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Sandeep Sanaboyina
Ranch Hand

Joined: Dec 14, 2009
Posts: 72
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

}

}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
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.


Ping & DNS - my free Android networking tools app
Sandeep Sanaboyina
Ranch Hand

Joined: Dec 14, 2009
Posts: 72
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
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
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

Joined: Oct 26, 2003
Posts: 418
    
    2

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 ?
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
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

Joined: Dec 14, 2009
Posts: 72
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Starting a thread without calling from main