forsome.java
来自「这一些程序都是实验之用」· Java 代码 · 共 48 行
JAVA
48 行
package occupycpu;
public class ForSome extends Thread{
private static StringBuffer log =new StringBuffer();
private static int count =0;
public void run(){
/* for(int i=0;i<20;i++)
{
log.append(currentThread().getName()+":"+i+" ");
if(++count%10==0)log.append("\n");
}*/
/*for(int i=0;i<20;i++)
{
log.append(currentThread().getName()+":"+i+" ");
if(++count%10==0)log.append("\n");
try{
sleep(100);
}catch(InterruptedException e){throw new RuntimeException(e);}
}
}*/
/*try{
sleep(60000);
System.out.println("sleep end");
}catch(InterruptedException e){System.out.println("sleep interrupted");}
System.out.println("end");
}*/
for(int a=0;a<20;a++)
{
log.append(currentThread().getName()+":"+a+" ");
if(++count%10==0)log.append("\n");
yield();
}
}
public static void main(String arg[])throws Exception{
ForSome forsome1=new ForSome();
ForSome forsome2=new ForSome();
forsome1.setName("r1");
forsome2.setName("r2");
forsome1.start();
forsome2.start();
while(forsome1.isAlive()||forsome2.isAlive())
{Thread.sleep(1000);}
System.out.println(log);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?