Java 带break语句的无限while循环
我是一名初学者,正在分析以下java代码:Java 带break语句的无限while循环,java,android,loops,infinite-loop,Java,Android,Loops,Infinite Loop,我是一名初学者,正在分析以下java代码: // t is a thread running while (true) { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } break; } t=null; 我想问的是:是否有必要将其放入一个无限循环中?因为正如我看到的,循环将只运行一次,也就是说,由于break语句。我需要一些解释。不,没有必要。您的观察
// t is a thread running
while (true) {
try {
t.join();
} catch (InterruptedException e) { e.printStackTrace(); }
break;
}
t=null;
我想问的是:是否有必要将其放入一个无限循环中?因为正如我看到的,循环将只运行一次,也就是说,由于break语句。我需要一些解释。不,没有必要。您的观察是正确的,循环将只执行一次 因此OP发布的代码等同于以下代码。
// t is a thread running
try {
t.join();
} catch (InterruptedException e) { e.printStackTrace(); }
t=null;
OP发布的代码:
// t is a thread running
while (true) {
try {
t.join();
} catch (InterruptedException e) { e.printStackTrace(); }
break;
}
t=null;
不,没有必要。您的观察是正确的,循环将只执行一次 因此OP发布的代码等同于以下代码。
// t is a thread running
try {
t.join();
} catch (InterruptedException e) { e.printStackTrace(); }
t=null;
OP发布的代码:
// t is a thread running
while (true) {
try {
t.join();
} catch (InterruptedException e) { e.printStackTrace(); }
break;
}
t=null;
t、 join();等待线程完成。之后,循环被您的“中断”中断。
=>始终仅循环1次=>不需要循环和中断,只需要t.join() t.join();等待线程完成。
之后,循环被您的“中断”中断。
=>始终仅循环1次=>不需要循环和中断,只需要t.join() 不需要while循环,因为t.join()等待线程死亡。 就像一个循环,在程序的
t.join()
行中,当线程没有死机时,程序将被阻塞
正确的代码是:
// t is a thread running
try {
t.join();
} catch (InterruptedException e) { e.printStackTrace(); }
t=null;
这里有一个例子:
不需要while循环,因为t.join()等待线程死亡。 就像一个循环,在程序的
t.join()
行中,当线程没有死机时,程序将被阻塞
正确的代码是:
// t is a thread running
try {
t.join();
} catch (InterruptedException e) { e.printStackTrace(); }
t=null;
这里有一个例子:
正如大家已经指出的那样,目前的代码是不正确的 但是,循环是必要的 正确的代码如下所示:
while (true) {
try {
t.join();
break;
} catch (InterruptedException e) { e.printStackTrace(); }
}
t = null;
如果没有循环,在当前线程成功加入之前,
t
有可能被设置为null。正如大家已经指出的那样,当前的代码是不正确的
但是,循环是必要的
正确的代码如下所示:
while (true) {
try {
t.join();
break;
} catch (InterruptedException e) { e.printStackTrace(); }
}
t = null;
如果没有循环,则在当前线程成功加入之前,
t
可以设置为null。循环不是必需的强>
据说线程t已经启动了。因此,“t=null”毫无意义。线程t已经启动,它将完成其工作。您可以在不使用while循环的情况下使用t.join()。在这种情况下,while循环没有意义。join方法允许一个线程等待另一个线程的完成。如果t是线程当前正在执行的线程对象
t、 join()
导致当前线程(下例中的主线程)暂停执行,直到t的线程终止
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();
}
}
}
}
公共类主{
公共静态void main(字符串[]args){
螺纹t=新螺纹(
新的Runnable(){
公开募捐{
对于(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+“--”+i);
试一试{
睡眠(100);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
});
t、 start();
试一试{
t、 join();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
t=null;//没有意义,t已经启动
对于(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+“--Thread--”+i);
试一试{
睡眠(100);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
不需要循环强>
据说线程t已经启动了。因此,“t=null”毫无意义。线程t已经启动,它将完成其工作。您可以在不使用while循环的情况下使用t.join()。在这种情况下,while循环没有意义。join方法允许一个线程等待另一个线程的完成。如果t是线程当前正在执行的线程对象
t、 join()
导致当前线程(下例中的主线程)暂停执行,直到t的线程终止
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();
}
}
}
}
公共类主{
公共静态void main(字符串[]args){
螺纹t=新螺纹(
新的Runnable(){
公开募捐{
对于(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+“--”+i);
试一试{
睡眠(100);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
});
t、 start();
试一试{
t、 join();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
t=null;//没有意义,t已经启动
对于(int i=0;i<100;i++){
System.out.println(Thread.currentThread().getName()+“--Thread--”+i);
试一试{
睡眠(100);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
}
这是什么代码>用于?我忘了删除它。谢谢。循环是必要的!这是什么代码>用于?我忘了删除它。谢谢。循环是必要的!中断的位置不对,但这是必要的。看我的回答,,below@G.BlakeMeike如果中断(在原始代码中)发生在t.join()调用之后,那么当然,我同意您的看法。我是艾尔