- 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();
- }
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/120520149524.html
来源: http://www.codesnippet.cn/detail/120520149524.html