同一个线程似乎是 运行 两次,但只在 Java 中被调用一次
Same thread seems as if it's running twice but is only invoked once in Java
我的 main 下有以下代码:
System.out.println(Thread.currentThread().getId());
for(inti=0;i!=Lock.totalThreads;i++) {
System.out.println("thread wascreated");
(new Thread(new MyThread())).start();
}
System.out.println("main finished running files");
MyThread class 看起来像这样:
public class MyThread implements Runnable {
private static int threadCounter=0;
private int myThreadId=0;
@Override
public void run() {
synchronized(Lock.lock){
threadCounter++;
myThreadId=threadCounter;
}
System.out.println("run()");
try {
runMe();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void runMe() throws IOException, InterruptedException {
String currentFile;
BufferedReader in;
System.out.println("run()");
switch(myThreadId){
case 1:
System.out.println("thread1 started");
System.out.println("thread1 finished");
System.out.println(Thread.currentThread().getId());
case 2:
System.out.println("thread2 started");
System.out.println("thread2 finished");
System.out.println(Thread.currentThread().getId());
}
}
和锁定 class看起来像这样:
public class Lock {
public static final Lock lock=new Lock();
public static final int totalThreads=1;
}
控制台输出是这样的:
1
thread was created
main finished running files
run()
runMe()
thread1 started
thread1 finished
8
thread2 started
thread2 finished
8
我很难理解为什么会发生这样的事情。
很明显(至少对我来说)只有一次创建线程(只有一次我们可以看到 运行(), 运行Me() and thread was created), but twice thread started/finished and the 输出中的线程 ID。
为什么 threadCounter 增加了两次,而只输入 运行() 一次?
P.S,我用的是Java6.
在 case 1
和 case 2
之后你还缺少 break;
。
我的 main 下有以下代码:
System.out.println(Thread.currentThread().getId());
for(inti=0;i!=Lock.totalThreads;i++) {
System.out.println("thread wascreated");
(new Thread(new MyThread())).start();
}
System.out.println("main finished running files");
MyThread class 看起来像这样:
public class MyThread implements Runnable {
private static int threadCounter=0;
private int myThreadId=0;
@Override
public void run() {
synchronized(Lock.lock){
threadCounter++;
myThreadId=threadCounter;
}
System.out.println("run()");
try {
runMe();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void runMe() throws IOException, InterruptedException {
String currentFile;
BufferedReader in;
System.out.println("run()");
switch(myThreadId){
case 1:
System.out.println("thread1 started");
System.out.println("thread1 finished");
System.out.println(Thread.currentThread().getId());
case 2:
System.out.println("thread2 started");
System.out.println("thread2 finished");
System.out.println(Thread.currentThread().getId());
}
}
和锁定 class看起来像这样:
public class Lock {
public static final Lock lock=new Lock();
public static final int totalThreads=1;
}
控制台输出是这样的:
1
thread was created
main finished running files
run()
runMe()
thread1 started
thread1 finished
8
thread2 started
thread2 finished
8
我很难理解为什么会发生这样的事情。
很明显(至少对我来说)只有一次创建线程(只有一次我们可以看到 运行(), 运行Me() and thread was created), but twice thread started/finished and the 输出中的线程 ID。
为什么 threadCounter 增加了两次,而只输入 运行() 一次?
P.S,我用的是Java6.
在 case 1
和 case 2
之后你还缺少 break;
。