public class Thread7 implements Runnable {


    public static void main(String r[]) {
        Thread t = new Thread(new Thread7());
        t.start();
        public void run() {
            System.out.println("C");
            System.out.println("D");
            }

        System.out.println("A");

            try {
                    t.join(); 
                    System.out.println("B"); }
            catch(Exception e){}
        }
}

Query1: On compiling this program I get "illegal start of expression" error pointing to public void run. But when I remove that block of code and place it outside main() the program compiles and runs successfully. What is the rule behind it?

Query2: After I place run() outside main() the output is ACDB. What I don't understand is after t.start() is invoked shouldn't the control transfer first to run() block and print CD first then followed by AB?

Dani AI

Generated

Short answers first.

You get "illegal start of expression" because Java does not allow you to declare a method directly inside another method. public void run() must be a class-member method (or be part of a local/anonymous class or lambda you create inside main). As discovered, moving run() out to the class level fixes the compile error. As pointed out, you must implement run() for Runnable, but one small correction: t.join() has nothing to do with daemon threads — join() simply makes the current thread wait until the thread t terminates.

A compact alternative that keeps the runnable inside main (Java 8+) is:

Thread t = new Thread(() -> {
    System.out.println("C");
    System.out.println("D");
});
t.start();   // schedules a new thread and returns immediately
// t.run();  // would execute on the current (main) thread instead

Why you saw ACDB: t.start() is asynchronous — it schedules the new thread and immediately returns control to main, so main continues and prints A. Later, when main reaches t.join() it blocks until the other thread finishes (printing C and D) and then prints B. If you want CD before A, call t.join() right after t.start() (or call t.run() to execute synchronously). If you want AB before CD, you'll need to coordinate (for example print A/B before starting the thread or use more explicit synchronization).

Small tips: catch the specific InterruptedException from join() instead of swallowing Exception, and for real work prefer ExecutorService rather than creating raw Thread objects.

The Runnable Interface requires that the class which implements it must over-ride the run() method.And yes,you are declaring a function immediately inside a function which is illegal.Hence run would need to be declared outside main and in the same level as main,i.e. in the class.

t.join() encsures that the daemon threads wait till the main thread is finished.It does not necessarily imply that the run method's statements will be invoked first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.