?? userunnable.java
字號:
import java.applet.Applet;
import java.awt.*;
public class UseRunnable extends Applet implements Runnable
{ Label l1=new Label("第一個子線程");
Label l2=new Label("第二個子線程");
TextField threadI=new TextField(16);
TextField threadII=new TextField(16);
Thread th1,th2;
int num1=0,num2=0;
public void init( )
{ add(l1);
add(threadI);
add(l2);
add(threadII);
}
public void start( )
{ th1=new Thread(this,"線程一");
th2=new Thread(this,"線程二");
th1.start( );
th2.start( );
}
public void stop( )
{ if(th1.isAlive( ))
th1.stop( );
if(th2.isAlive( ))
th2.stop( );
}
public void run( )
{ String str;
while(true)
{ try
{ Thread.sleep((int)(Math.random( )*3000));
}
catch(InterruptedException e) { }
str=Thread.currentThread( ).getName( );
if(str.equals("線程一"))
{ num1++;
threadI.setText("線程一第"+num1+"次被調(diào)用");
}
else if(str.equals("線程二"))
{ num2++;
threadII.setText("線程二第"+num2+"次被調(diào)用");
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -