线程实例2

public class threadDemo1 extends Thread {
int flag;
public threadDemo1(String name, int f) {
super(name);
this.flag = f;
}

public void run() {
char ch;
System.out.println();
System.out.print(getName() + " start: ");
synchronized ("ss") {
if (flag == 0) {

for (ch = 'a'; ch <= 'z'; ch++)
System.out.print(ch + " ");
}

else if (flag == 1) {
for (ch = 'A'; ch <= 'Z'; ch++)
System.out.print(ch + " ");
}
System.out.print(getName() + " end!");
}
}

public static void main(String[] args) {
threadDemo t1 = new threadDemo("线程1", 1);
threadDemo t2 = new threadDemo("线程2", 0);
// System.out.println("active: "+Thread.activeCount());
t1.start();
t2.start();
System.out.println("active: " + Thread.activeCount());
}
}

猜你喜欢

转载自fxy701.iteye.com/blog/1172142