demothread.java

来自「写给JSP初级程序员的书」· Java 代码 · 共 72 行

JAVA
72
字号
package jdeveloper.study;/** * Title:        Jdeveloper's Java Projdect * Description:  n/a * Copyright:    Copyright (c) 2001 * Company:      soho  http://www.ChinaJavaWorld.com * @author jdeveloper@21cn.com * @version 1.0 */import java.lang.Runnable;import java.lang.Thread;public class DemoThread implements Runnable{  public DemoThread() {         TestThread testthread1 = new TestThread(this,"1");         TestThread testthread2 = new TestThread(this,"2");         testthread2.start();         testthread1.start();  }  public static void main(String[] args) {    DemoThread demoThread1 = new DemoThread();  }   public void run(){        TestThread t = (TestThread) Thread.currentThread();        try{          if (!t.getName().equalsIgnoreCase("1")) {              synchronized(this) {                  wait();              }          }          while(true){            System.out.println("@time in thread"+ t.getName()+ "="+ t.increaseTime());            if(t.getTime()%10 == 0) {              synchronized(this) {                System.out.println("****************************************");                this.notifyAll();                if ( t.getTime()==100 ) break;                wait();            }          }        }        }catch(Exception e){e.printStackTrace();}    }}class TestThread extends Thread{    private int time = 0 ;    public TestThread(Runnable r,String name){      super(r,name);    }    public int getTime(){       return time;    }    public int increaseTime (){       return ++time;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?