Java中的while(true)循环如果没有System.out.println语句,将永远不会结束。

huangapple go评论66阅读模式
英文:

Java while(true) loop never ends without a System.out.println

问题

我有以下代码其中包含一个ArrayList该ArrayList在另一个Java类中填充该类是一个线程我每次都检查它总是被填充的),然后在主类中有有问题的**while(true)**代码块问题是如果我注释或删除**System.out.println(IDS.size());**这一行它永远不会结束

尽管线程负责完成我需要的信息但在它们全部完成之前我不能显示结果这就是使用while(true)的原因

// 这个ArrayList在其他类中被正确地填充(每个线程都会添加一个元素,总共10个)
public static ArrayList<String> IDS = new ArrayList<String>();

// 这里是问题所在
while (true) {
    // 如果我注释掉下面的system.out.println这一行
    // 循环将永远不会结束,也不会中断
    System.out.println(IDS.size());
    if (IDS.size() == 10) {
        break;
    }
}

// 当数组填充有10个元素时,我显示所有信息
for (int k = 0; k < impresoras.size(); k++) {
    System.out.println(impresoras.get(k).ID);
}

我不知道为什么会出现这种情况有人能帮忙吗
提前谢谢
英文:

I have the following code with an ArrayList that is filled in other Java class that is a Thread (this is always filled, I have checked every time), then I have in the Main class the problematic block while(true) and the issue is that never ends if I comment or delete the line System.out.println(IDS.size());

Although are Threads in charge of complete the information I need, I cant show the results until all of them are finished. This is the reason of while(true) block.

public static ArrayList&lt;String&gt; IDS = new ArrayList&lt;String&gt;();
//this arraylist is filled in other classes correctly (each Thread add a element -&gt; 10 in total)


//here is the problem
while (true) {
    //if I comment the next system.out.println line
    //the loop never ends and never breaks
    System.out.println(IDS.size());
    if(IDS.size()==10) {
    	break;
    }
 }

//when the array is filled with the 10 elements, I show all the info
for (int k = 0; k &lt; impresoras.size(); k++) {
	System.out.println(impresoras.get(k).ID);	
}

I don´t know why this is happening, can someone helps?
Thanks in advance.

答案1

得分: 0

最后,我使用线程的join方法来避免使用while true循环。所以,我只需调用next方法,然后在所有线程完成工作后执行System.out.println来打印我的项目,这些项目将在所有线程结束工作时打印出来。

public static ArrayList<MyThread> threadList = new ArrayList<MyThread>();

public static void openFileAndCreateThreads(String fileName) {
    String line = null;
    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    try {
        fileReader = new FileReader(fileName);
        bufferedReader = new BufferedReader(fileReader);

        while ((line = bufferedReader.readLine()) != null) {
            String[] line_parts = line.split(";");
            
            MyThread t = new MyThread(line_parts[0].trim(), line_parts[1], line_parts[2]);
            
            threadList.add(t);
            t.start();
        }
        for (int j = 0; j < threadList.size(); j++) {
            try {
                threadList.get(j).join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            bufferedReader.close();
        } catch (Exception ex) {
        }
    }
}
英文:

Finally I use the join methods for the Threads to avoid the while true loop.
So only I have to call the next method and then do the System.out.println of my items, that will be printed when all Threads ends their work.

public static ArrayList&lt;MyThread&gt; threadList = new ArrayList&lt;MyThread&gt;();

public static void openFileAndCreateThreads(String fileName) {
		String line = null;
		FileReader fileReader = null;
		BufferedReader bufferedReader = null;
		try {
			fileReader = new FileReader(fileName);
			bufferedReader = new BufferedReader(fileReader);

			while ((line = bufferedReader.readLine()) != null) {
				String[] line_parts = line.split(&quot;;&quot;);

				MyThread t= new MyThread(line_parts[0].trim(), line_parts[1], line_parts[2]);
				
				threadList.add(t);
				t.start();
			}
			for (int j=0;j&lt;threadList.size();j++) {
				try {
					threadList.get(j).join();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
			}
			bufferedReader.close();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();

		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				bufferedReader.close();
			} catch (Exception ex) {
			}
		}
	}

huangapple
  • 本文由 发表于 2020年8月26日 19:12:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63596407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定