Solved

Java Thread

Posted on 2007-04-08
1
230 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 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

20 Experts available now in Live!

Get 1:1 Help Now