user3740494
user3740494

Reputation: 21

Infinite while loop with break statement

I am a beginner and I was analyzing this java code:

// t is a thread running
while (true) {
    try {
        t.join(); 
    } catch (InterruptedException e) { e.printStackTrace(); }
    break;
}
t=null;

What I am asking is: is there a need to put that inside an infinite loop? Because as I see the loop will run only once i.e due to that break statement. I need some explanation please.

Upvotes: 1

Views: 1433

Answers (5)

peter.petrov
peter.petrov

Reputation: 39457

No, there's no need. Your observation is correct, the loop will be executed only once.

So the OP-posted code is equivalent to the following code.

// t is a thread running
try {
    t.join(); 
} catch (InterruptedException e) { e.printStackTrace(); }
t=null;

Code posted by OP:

// t is a thread running
while (true) {
    try {
        t.join(); 
    } catch (InterruptedException e) { e.printStackTrace(); }
    break;
}
t=null;

Upvotes: 2

indika
indika

Reputation: 923

Loop is not necessary!. It is said that thread t is already started. Therefore it makes no sense "t=null". Thread t is already started and it will finish its job. You can use t.join() without while loop. In this context while loop does not make sense. The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,

t.join();

causes the current thread(main thread in below example) to pause execution until t's thread terminates.Run following code snippet and know the ropes.

public class Main {

public static void main(String[] args) {

    Thread t=new Thread(
         new Runnable() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    System.out.println(Thread.currentThread().getName()+"--"+i);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });

    t.start();

    try {
        t.join();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    t = null;  // no sense, t is already started

    for (int i = 0; i < 100; i++) {
        System.out.println(Thread.currentThread().getName()+"--Thread--"+i);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

Upvotes: 0

G. Blake Meike
G. Blake Meike

Reputation: 6715

As everyone has already pointed out, the code is incorrect as it stands.

The loop, however is necessary!

The correct code looks like this:

while (true) {
    try {
        t.join();
        break; 
    } catch (InterruptedException e) { e.printStackTrace(); }
}
t = null;

Without the loop, it is possible for t to be set to null, before the current thread successfully joins it.

Upvotes: 1

user3636812
user3636812

Reputation:

No need to have a while loop, because t.join() waits for the thread to die. is like a loop already, in the program at the line of t.join() the program will be blocked while the thread is not dead.

The correct code is:

// t is a thread running
    try {
        t.join(); 
    } catch (InterruptedException e) { e.printStackTrace(); }

    t=null;

Here you have an example :

http://www.tutorialspoint.com/java/lang/thread_join.htm

Upvotes: 0

tana
tana

Reputation: 585

t.join(); waiting the finishing of thread.
After that, loop is broken by your "break".
=> always loop only 1 time => no need loop and break, only need t.join();

Upvotes: 0

Related Questions