Solved

Java Thread

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 80
object oriented programming comparison 5 77
CSV file parsing thru Java 13 34
collection output issue 9 36
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 Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

790 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