我的目标是启动三个未同步的线程,并让它们各添加一千个元素。这些元素需要存储在一个集合中(我选择了Vector),然后打印出来。出于某种原因,我没有得到要打印的元素。我在尝试解决打印方法和创建元素的方法时遇到了麻烦。任何帮助表示赞赏。
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
public class MyRun implements Runnable{
int global = 0;
//create sets
static Vector t1v = new Vector();
static Vector t2v = new Vector();
static Vector t3v = new Vector();
public void run() {
action1();
}
public void action1() {
for(int i = 0; i < 1000; i++) {
global++;
int x = global;
String tName = Thread.currentThread().getName();
if(tName == "t1") {
t1v.addElement(x);
System.out.println(t1v.elementAt(x));
}
if(tName == "t2") {
t2v.addElement(x);
}
if(tName == "t3") {
t3v.addElement(x);
}
//System.out.println("thread: " + tName + "counter is: " + global);
}
}
public static Vector gett1v() {
return t1v;
}
public static Vector gett2v() {
return t2v;
}
public static Vector gett3v() {
return t3v;
}
}
class ThreadTest{
public static void main(String[] args) {
//create threads
Runnable threadJob = new MyRun();
Thread t1 = new Thread(threadJob);
t1.setName("thread1");
Thread t2 = new Thread(threadJob);
t2.setName("thread2");
Thread t3 = new Thread(threadJob);
t3.setName("thread3");
t1.start();
t2.start();
t3.start();
Vector th1v = MyRun.gett1v();
Vector th2v = MyRun.gett2v();
Vector th3v = MyRun.gett3v();
//MyRun.printSets();
for(int i=0; i < th1v.size(); i++) {
System.out.println("Thread 1: " + th1v.elementAt(i));
}
for(int i=0; i < th2v.size(); i++) {
System.out.println("Thread 2: " + th2v.elementAt(i));
}
for(int i=0; i < th3v.size(); i++) {
System.out.println("Thread 3: " + th3v.elementAt(i));
}
}
}
答案 0 :(得分:1)
我冒昧地修改了你的代码。
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
public class MyRun implements Runnable{
AtomicInteger global = new AtomicInteger(0);
//create sets
static Vector t1v = new Vector();
static Vector t2v = new Vector();
static Vector t3v = new Vector();
public void run() {
action1();
}
public void action1() {
for(int i = 0; i < 1000; i++) {
global.incrementAndGet();
int x = global.get();
String tName = Thread.currentThread().getName();
if(tName == "t1") {
t1v.addElement(x);
}
if(tName == "t2") {
t2v.addElement(x);
}
if(tName == "t3") {
t3v.addElement(x);
}
//System.out.println("thread: " + tName + "counter is: " + global);
}
}
public static Vector gett1v() {
return t1v;
}
public static Vector gett2v() {
return t2v;
}
public static Vector gett3v() {
return t3v;
}
}
class ThreadTest{
public static void main(String[] args) {
//create threads
Runnable threadJob = new MyRun();
Thread t1 = new Thread(threadJob);
t1.setName("t1");
Thread t2 = new Thread(threadJob);
t2.setName("t2");
Thread t3 = new Thread(threadJob);
t3.setName("t3");
t1.start();
t2.start();
t3.start();
try{
t1.join();
t2.join();
t3.join();
}catch(InterruptedException e){}
Vector th1v = MyRun.gett1v();
Vector th2v = MyRun.gett2v();
Vector th3v = MyRun.gett3v();
//MyRun.printSets();
System.out.println(th1v.size()+" "+th2v.size()+" "+th3v.size());
for(int i=0; i < th1v.size(); i++) {
System.out.println("Thread 1: " + th1v.elementAt(i));
}
for(int i=0; i < th2v.size(); i++) {
System.out.println("Thread 2: " + th2v.elementAt(i));
}
for(int i=0; i < th3v.size(); i++) {
System.out.println("Thread 3: " + th3v.elementAt(i));
}
}
}
注意事项:
主线程独立于t1,t2,t3线程,所以如果你想 获取结果使用join()或使用FutureTask或 ExecutorService.submit(Runnable r)。所以主线程将等待 所有三个线程完成然后执行for循环。 由于主线程没有等待,你的输出空了。
如果它们是多个mutator线程,则使用Atomic类型,但只有一个mutator线程,而且多个reader线程使用volatile关键字,但使用volatile关键字代价很高。
希望有所帮助!!