合肥学院java实验十
- 格式:doc
- 大小:50.50 KB
- 文档页数:6
{
count++;
no=count;
}
public void run()
{
try
{
for (int i=0;i<10;i++)
{
delay=(int)(Math.random()*5000);
Thread.sleep(delay);
System.out.println("Thread"+no+"with a delay "+delay);
2.程序必须能够实现多线程;
3.程序必须能够完成题目要求;
4.写出实验报告。
三、实验内容及过程:(宋体小四,)
1.阅读下列程序,分析并上机检验其功能。
class DelayThread exends Thread{
private static int count=0;
private int no;
private int delay;
thread1.start();
thread2.start();
try{ Thread.sleep(1000);}catch(InterruptedException e){
System.out.println(“Thread wrong”);}}}
2.讲上列程序利用Runnable接口改写,并上机检验。
}
}catch(InterruptedException e){}}}
public class MyThread{
public static void main(String args[]){
DelayThread thread1=new DelayThread();
DelayThread thread2=new DelayThread();
}
}
}
3.
注:报告内容不得雷同,否则视为抄袭,无成绩
public DelayThread(){
count++;
no=count;
}
public void run(){
try{
for (int i=0;i<10;i++){
delay=(int)(Math.random()*5000);
sleep(delay);
System.out.println(“Thread ”+no+” with a delay ”+delay);
3.利用多线程编写一个模拟时钟(图形界面程序、Runnable接口),有时分秒针
编写一个应用程序,创建三个线程分别显示各自的时间。
四、实验中的问题思考及讨论:(列出你实验中出现的问题,及解决的问题和解决的思路)
不是很清楚Thread和Runnable的相互转化,通过问同学加以解决。
五、实验总结:(简单、准确的总结本次实验的主要内容,个人收获)
本次试验主要用Thread类和Runnable接口来建立线程。
六、源代码及运行结果
1.运行结果如下:
2.
class Delayrunnnable implements Runnable
{
private static int count=0;
private int no;
private int delay;
}
}
catch(InterruptedException e)
{
}
}
}ቤተ መጻሕፍቲ ባይዱ
public class MyRunnable
{
public static void main(String args[])
{
Delayrunnnable r1=new Delayrunnnable();
Delayrunnnable r2=new Delayrunnnable();
《面向对象程序设计》实验报告
合肥院学院计算机科学与技术系
实验名称:Java多线程
日期:
姓名/学号:专业/班级:指导老师:成绩:
一、实验目的:(详见指导书,宋体小四)
熟悉利用Thread类建立多线程方法。
熟悉利用Thread接口建立多线程方法。
二、实验要求:(详见指导书,宋体小四)
1.通过实验掌握Thread、Runnable使用方法;
Thread thread1=new Thread(r1);
Thread thread2=new Thread(r2);
thread1.start();
thread2.start();
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
System.out.println("Thread wrong");