Eugene
Eugene

Reputation: 60184

One thread is always running without a chance for other threads

Why the main thread is never executed? I thought that is I use Thread.sleep(int value) I give a chance to other threads to run, but this never happen.

    public static void main(String[] args) {
        final Sook o = new Sook();
        Thread t = new Thread(new Runnable() {
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(10000); // Specially set to give a chance to the main thread to run
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        t.run();

        System.out.println("<<<<<BACK TO MAIN >>>>>>"); // Never happens
    }

Upvotes: 1

Views: 1044

Answers (1)

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

Do not call, t.run(), call t.start()

Just run will call the run method in the currently Thread.

Upvotes: 12

Related Questions