public class ThreadTest implements Runnable {
public static int a = 0;
public void run() {
for (int k = 0; k < 5; k++) {
a = a + 1;
}
}
public static void main(String[] args) throws Exception {
Runnable r = new ThreadTest();
Thread t = new Thread(r);
t.start();
t.join(); //主要用于等待t线程运行结束,若无此句,main则会执行完毕,导致结果不可预测
System.out.println(a);
}
}
public static int a = 0;
public void run() {
for (int k = 0; k < 5; k++) {
a = a + 1;
}
}
public static void main(String[] args) throws Exception {
Runnable r = new ThreadTest();
Thread t = new Thread(r);
t.start();
t.join(); //主要用于等待t线程运行结束,若无此句,main则会执行完毕,导致结果不可预测
System.out.println(a);
}
}