Solved

Java Thread

Posted on 2007-04-08
1
226 Views
Last Modified: 2010-03-31
Hi Experts,
While using java threads, can a thread be executed before the main is executed, reason?

Thanks
0
Comment
Question by:SGIT
1 Comment
 
LVL 10

Accepted Solution

by:
Jaax earned 500 total points
ID: 18874574
If you mean can user-defined threads be called before main, then the answer is no, as the main method is entry point for a java application and all threads needs to be started from some java method.

If you mean can user-defined threads be completed before main, then the answer is yes, and jvm will only exit after all user-defined threads have been run.

However, you can run a thread without a main method as in the following example, before jvm throws
"java.lang.NoSuchMethodError: main". To avoid the error use System.exit(0) as the last statement

public class Try {
    static {
        System.out.println("Inside Static Block");
        Thread t = new userThread();
        t.start();
        System.exit(0);
    }
}

class userThread extends Thread {
    public void run() {
        System.out.println("Inside the userThread");
    }
}
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now