三个java程序--join用法
//NO.1
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) {
Runnable r = new ThreadTest();
Thread t = new Thread(r);
t.start();
System.out.println(a);
}
}
-----------------------------------------------------------------------
//NO.2
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();
System.out.println(a);
}
}
-------------------------------------------------------------------
NO.3
public class ThreadTest implements Runnable {
public void run() {
for (int k = 0; k < 10; k++) {
System.out.print(" " + k);
}
}
public static void main(String[] args) throws Exception {
Runnable r = new ThreadTest();
Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
t1.start();
t1.join();
t2.start();
}
}