【发布时间】:2016-02-16 07:18:08
【问题描述】:
我想编写一个程序,在 main 方法中运行 3 个并发线程,该方法使用无限循环将字词添加到类型为 ArrayList<String> 的静态字段 infiList。
我知道如何创建线程并运行它们,但我不知道如何将它们添加到infiList。任何帮助将不胜感激:)
import java.util.ArrayList;
public class Multithreading implements Runnable {
public Multithreading () {
//
}
// Implement the run method in Runnable
public void run() {
// Tell system how to run custom thread
}
public static int infiList(ArrayList<String>) {
return t1;
}
public static void main (String args[]) {
// Create an instance of the class
Multithreading task = new Multithreading();
// Create a thread
Thread t1 = new Thread(task);
Thread t2 = new Thread(task);
Thread t3 = new Thread(task);
// Start a thread
t1.start();
t2.start();
t3.start();
}
}
【问题讨论】:
-
“我知道如何创建线程并运行它们”我希望你意识到,在你的代码中你没有启动一个
Thread?顺便提一句。如果你实现Runnable,你需要覆盖run-方法 -
什么是
t1?你的代码没有意义,你为什么要发布一些甚至无法编译的东西?
标签: java arrays multithreading loops static