TwoThreadSleep.java
资源名称:Source.rar [点击查看]
上传用户:songled
上传日期:2022-07-14
资源大小:94k
文件大小:1k
源码类别:
进程与线程
开发平台:
Java
- public class TwoThreadSleep extends Thread {
- public void run() {
- loop();
- }
- public void loop() {
- // get a reference to the thread running this
- Thread t = Thread.currentThread();
- String name = t.getName();
- System.out.println("just entered loop() - " + name);
- for ( int i = 0; i < 10; i++ ) {
- try {
- Thread.sleep(200);
- } catch ( InterruptedException x ) {
- // ignore
- }
- System.out.println("name=" + name);
- }
- System.out.println("about to leave loop() - " + name);
- }
- public static void main(String[] args) {
- TwoThreadSleep tt = new TwoThreadSleep();
- tt.setName("my worker thread");
- tt.start();
- // pause for a bit
- try {
- Thread.sleep(700);
- } catch ( InterruptedException x ) {
- // ignore
- }
- tt.loop();
- }
- }