Solved

Java Thread

Posted on 2007-04-08
1
235 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

687 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