英文:
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<String> IDS = new ArrayList<String>();
//this arraylist is filled in other classes correctly (each Thread add a element -> 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 < 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<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) {
			}
		}
	}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论