Solved

Java Thread

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 75
What's wrong with this code? 4 44
Java Eclipse Loop 3 45
Find out all repos that a user is most active on Github 1 52
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

738 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