注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

尝试将复杂的问题简单化-zzy

纸上得来终觉浅,绝知此事要躬行。

 
 
 

日志

 
 
关于我

教书是职业的要求,教学是份内的事,要努力做一个好教师.

网易考拉推荐

<oop> 实验十七 多线程程序设计  

2007-12-24 17:58:18|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

 17.1  实验目的

1、理解什么是多线程程序设计;

2、了解JAVA语言的多线程实现机制;

3、掌握通过继承Thread类实现客户端多线程程序设计;

4、了解多线程的“生命周期”机制。

17.2实验内容

1、  读懂下列程序,并调试运行,写出程序执行结果。

//: c13:SimpleThread.java

// Very simple Threading example.

public class SimpleThread extends Thread {

  private static Test monitor = new Test();

  private int countDown = 5;

  private static int threadCount = 0;

  public SimpleThread() {

    super("" + ++threadCount);   // Store the thread name

    start();

  }

  public String toString() {

    return "#" + getName() + ": " + countDown;

  }

  public void run() {

    while(true) {

      System.out.println(this);

      if(--countDown == 0) return;

    }

  }

  public static void main(String[] args) {

    for(int i = 0; i < 5; i++)

      new SimpleThread();

   }

} ///:~

 

 

2、  将以下两段文字翻译成中文:

(1)If you know that you’ve accomplished what you need to in your run( ) method, you can give a hint to the thread scheduling mechanism that you’ve done enough and that some other thread might as well have the CPU. This hint (and it is a hint—there’s no guarantee your implementation will listen to it) takes the form of the yield( ) method.

(2)By using yield( ), the output is evened up quite a bit. But note that if the output string is longer, you will see output that is roughly the same as it was in SimpleThread.java (try it—change toString( ) to put out longer and longer strings to see what happens). Since the scheduling mechanism is preemptive, it decides to interrupt a thread and switch to another whenever it wants, so if I/O (which is executed via the main( ) thread) takes too long, it is interrupted before run( ) has a chance to yield( ). In general, yield( ) is useful only in rare situations, and you can’t rely on it to do any serious tuning of your application.

17.3 请简述您对JAVA“多线程”的理解。

 

  评论这张
 
阅读(667)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017