📄 xiancheng.java
字号:
package zhaofei;
public class xiancheng{
public static void main(String[] args) {
int n = 0;
SimpleThread zf1 = new SimpleThread("1");
SimpleThread zf2 = new SimpleThread("2");
zf1.setPriority(1);
zf2.setPriority(5);
//for (int i=0 ;i<5; i++){
zf1.start(); //通过休眠 分别的执行 线程1 和2
zf2.start();
//}
//n = zf1.n;
//n = zf2.n;
//while (zf1.i <1) {
// n++;//System.out.println(zf1.i);
//}
System.out.println(n);
}
}
class SimpleThread extends Thread{
int i,n=0;
public SimpleThread(String str){
super(str);
}
public synchronized void run(){ //就在这里处理问题
for (this.i=0; i<8;i++){
System.out.println("<<" +i+" 线程" +getName()+">>");
try{
sleep((long)(Math.random()*50)); //sleep 指定进程执行多久休眠
// sleep(100);
}
catch (InterruptedException e){}
}
n=Integer.parseInt(getName());
System.out.println(getName()+ "完成" + this.n);
//n++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -