我是多线程的新手,这有点令人困惑。我怎样才能使用同步键给出输出: 1 a 然后 2 b ?换句话说,我希望第一个线程访问静态变量 x,递增它,然后启动第二个线程。
public class Test1 extends Thread{
static int x=0;
String name;
Test1(String n){ name=n;}
public void increment() {
x=x+1;
System.out.println(x+" "+ name);
}
public void run() {
this.increment();
}
}
public class Main {
public static void main(String args[]) {
Test1 a= new Test1("a");
Test1 b= new Test1("b");
a.start();
b.start();
}
}