⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 31.html

📁 写给JSP初级程序员的书
💻 HTML
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="description" content="Java,JDBC,EJB,Open Source,jdk,rmi">
<meta name="Keywords"
content="Java, servlets, Java servlet, Javascript, ActiveX, VRML,
        applet, applets, directory, news, jdbc, applications, 
        Java applications, Java developer, Java development, developer, 
        classes, Jars.com, Jars, intranet, Java applet, Javabeans, 
        Java products, JDK, Java development kit, java development environment, JIT,
        JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, 
        SUN, CGI, Perl, database, network, html,
        xml, dhtml, rating, ratings, review, jars, cgi, programming,
        software review, software rating">
<title>csdn_线程基础---wait(),notify的应用一例</title>
<style>
.news {   BACKGROUND: #007cd3;  font-family: "宋体"; font-size: 9pt }
.t {  font-family: "宋体"; font-size: 9pt }
.t1 { color:#007cd3;  font-family: "宋体"; font-size: 9pt }
.white { font-family: "宋体"; font-size: 9pt;color:#FFFFFF }
.red { font-family: "宋体"; font-size: 9pt;color:#FF0000 }
A:visited {color:#0000FF}
A:hover {color: #ff6666; text-decoration: none}
.text {font-size: 12px; line-height: 160%; font-family: "宋体"}
.text1 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text1:visited {color:#000000}
.text1:hover {color: #000000}
.text2 {color:#000000; font-size: 12px; line-height: 130%; font-family: "宋体"; text-decoration: none}
.text2:visited {color:#000000}
.text2:hover {color: #000000}
.text3 {font-size: 12px; line-height: 100%; font-family: "宋体"; text-decoration: none}
.large {font-size: 14.8px; line-height: 130%}
</style>
</head>
<body


<center>




  <tr>
    <td WIDTH="100%" VALIGN="TOP">
      <tr>
        <td WIDTH="100%" CLASS="white"></td>
      </tr>
    
      <tr>
        <td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>线程基础---wait(),notify的应用一例</td>
     <p>   <td WIDTH="50%" bordercolor="#FFFFFF" CLASS="t1" bgcolor="#F0F0F0" align="center" nowrap>作者:jdeveloper</td></p>
      </tr>
      <tr>
        <td WIDTH="100%" bordercolor="#FFFFFF" CLASS="t" bgcolor="#F0F0F0" colspan="2">
<pre>
本例子实现了两个线程,每个线程输出1到100的数字。
第一个线程输出1-10,停止,通知第二个线程 输出1-10 第二个线程停止 通知第一个线程 输出11-20 ...
实现的要点是
  在Java中,每个对象都有个对象锁标志(Object lock flag)与之想关联,当一个线程A调用对象的一段synchronized代码时,
  它首先要获取与这个对象关联的对象锁标志,然后执行相应的代码,执行结束后,把这个对象锁标志返回给对象;因此,在线程A执行
  synchronized代码期间,如果另一个线程B也要执行同一对象的一段synchronized代码时(不一定与线程A执行的相同),它将
  要等到线程A执行完后,才能继续....
  
  如何利用wait() notify() notifyAll()?
  
  在synchronized代码被执行期间,线程可以调用对象的wait()方法,释放对象锁标志,进入等待状态,并且可以调用notify()或者
  notifyAll()方法通知正在等待的其他线程。notify()通知等待队列中的第一个线程,notifyAll()通知的是等待队列中的所有线程。
  
  
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("****************************************");
                notify();
                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;
    }


}

<a href="DemoThread.java" tppabs="http://www.chinajavaworld.com/doc/lang/DemoThread.java">下载源程序</a>        
  运行方法 (for windows):
  javac -d . DemoThread.java
  java -classpath .;%classpath% jdeveloper.study.DemoThread
  
  
   

</pre>

        </td>
      </tr>
    </td>
  </tr>
</div>
</body>
</html>

⌨️ 快捷键说明

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