- 1、本文档共5页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
实验六:java多线程编程
计算机科学系实验报告
实验六:java多线程编程
一 、实验目的
1.掌握 Java 多线程编程方法.
二 、实验仪器设备与实验环境条件
1、PC机
2、Windows 7系统
3、Inernet连接
4、Eclipse开发平台
三 、实验内容、步骤、结果
Modify SimpleThread.java so that all the threads are daemon threads and verify that the program ends as soon as main(?) is able to exit
Create two Thread subclasses, one with a run(?) that starts up and then calls wait(?). The other class’s run(?) should capture the reference of the first Thread object. Its run(?) should call notifyAll(?) for the first thread after some number of seconds have passed so that first thread can print a message.
Inherit a class from java.util.Timer and implement the requestStop(?) method as in Stopping.java.
源代码:
1).
package MyThread;
public class SimpleThread extends Thread{
private int countDown = 5;
private static int threadCount = 0;
public SimpleThread() {
super( + ++threadCount);
}
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) {
SimpleThread simple = null;
for(int i = 0; i 5; i++){
simple=new SimpleThread();
simple.setDaemon(true);
simple.start();
}
}
}
实验结果截图
2).
package MyThread;
public class TwoThread extends Thread{
public TwoThread() {
}
public static void main(String[] args) {
ThreadFirst first = new ThreadFirst();
System.out.println(Start Thread First!);
first.start();
ThreadSecond second = new ThreadSecond(first);
System.out.println(Start Thread Second!);
second.start();
System.out.println(Main Thread Exit!);
}
}
class ThreadFirst extends Thread {
public void run() {
try {
Thread.sleep(1000);
waitMethod();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void waitMethod() {
try {
System.out.println
文档评论(0)