代码片段转自
http://2java.net/
public class ThreadTest {
public static Printer printer;
public static void main(String[] args) {
printer = new Printer();
new Thread(new Runnable() {
@Override
public void run() {
printer.print(1);
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
printer.print(2);
}
}).start();
}
public static class Printer {
private int c;
public void print(int i) {
int j = i;
c = j;
int count = 10;
while (count-- > 0) {
System.out.println("i=" + i);
System.out.println("j=" + j);
System.out.println("c=" + c);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}