广州大学学生实验报告
开课学院及实验室:计算机科学与工程实验室 2014年12月28日
一、实验目的
1. 掌握线程的概念、线程的生命周期。
2. 掌握采用继承Thread 类创建子线程。
3. 掌握使用Runnable 接口使类线程化。
二、实验器材
1.MacBook Pro一台
2.操作系统:OS X Yosemite
3.编程软件:eclipse
三、实验内容及原理
一 继承线程类
public class MultiThreadExample{
public static void main(String []args){
new MyThread("A").start();//启动线程A
new MyThread("B").start();//启动线程B
}
}
class MyThread extends Thread{
public MyThread(String n){
super(n); //线程名称
}
public void run(){
for(int i=0;i<5;i++){
try{
// 睡眠一段随机时间
Thread.sleep((long)(Math.random() * 1000));
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.print(getName()); //打印线程名称
}
}
}
二、实现Runnable接口
public class MultiThreadExample2{
public static void main(String []args){
Thread t1=new Thread(new CustomThread("A"));
Thread t2=new Thread(new CustomThread("B"));
t1.start();//启动线程A
t2.start();//启动线程B
}
}
class CustomThread implements Runnable{
String name;
public CustomThread(String n){
name=n;
}
public void run(){
Thread current=Thread.currentThread();//取得当前线程
for(int i=0;i<5;i++){
try{
// 睡眠一段随机时间
current.sleep((long)(Math.random() * 1000));
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.print(name); //打印线程名称
}
}
}
三 模拟小球平抛和自由落体的
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class BallThread extends Applet implements Runnable{
Thread red, blue;
Graphics redPen, bluePen;
int t=0;
public void init(){
red = new Thread(this);
blue = new Thread(this);
redPen = getGraphics();
bluePen = getGraphics();
redPen.setColor(Color.red);
bluePen.setColor(Color.blue);
}
public void start(){
red.start();
blue.start();
}
public void run(){
while(true){
t=t+1;
if(Thread.currentThread()==red){
if(t>100)t=0;
redPen.clearRect(0,0,110,400);
redPen.fillOval(50,(int)(1.0/2*t*9.8),15,15);
try{
red.sleep(40);
}catch(InterruptedException e){}
}else if(Thread.currentThread()==blue){
bluePen.clearRect(120,0,900,500);
bluePen.fillOval(120+7*t,(int)(1.0/2*t*9.8),15,15);
try{
blue.sleep(40);
}catch(InterruptedException e){}
}
}
}
}
四、实验过程原始数据记录
1. 模拟小球平抛和自由落体的
五、实验结果及分析
通过这次实验,学会了java中多线程的应用,主要有2种方式实现,一种是子类化Thread,另一种是实现Runnable协议。
第二篇:实验八 JAVA多线程
实验八 多线程
? 实验目的
1.线程的概念。
2.掌握使用Tread类和Runnable接口创建线程。
? 实验环境
JDK1.4以上版本, Eclipse集成开发环境
? 实验内容
1. 编写一个应用程序创建两个线程,一个线程打印输出1~1000之间所有的奇数,另外一个线程打印输出1~1000之间所有的偶数,要求两个线程随机休眠一段时间后继续打印输出下一个数。分别使用Runnable接口和Tread类编程实现。
实验1比较简单,自己完成!
2.选择两个城市作为预选旅游目标,实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000毫秒以内),哪个先显示完毕,就决定去哪个城市。请分别用Runnable接口和Thread类实现。
下面是用Thread类的实现,参考程序如下,用Runable接口实现方法类似。 public class Ex82 {
public static void main(String[] args){
String[] citys = {"合肥","淮南"};
ThreadCity1 thread1 = new ThreadCity1(citys);
ThreadCity2 thread2 = new ThreadCity2(citys);
thread1.start();
thread2.start();
/*
* 请在下面补充几行代码,保证thread1和thread2都允许结束再执行后面的if语句。 */
While(thread1.isAlive() || thread2.isAlive()){
}
if(thread1.getTime()<thread2.getTime()){
System.out.println("决定去:"+citys[0]);
}
else{
1
System.out.println("决定去:"+citys[1]); }
}
}
class ThreadCity1 extends Thread{
}
class ThreadCity2 extends Thread{
private String[] citys; private long time; ThreadCity2(String[] citys){ } public long getTime(){ } return time; this.citys=citys; private String[] citys; private long time; public long getTime(){ } ThreadCity1(String[] citys){ } public void run(){ long start = System.currentTimeMillis(); for(int i=0;i<10;i++){ } try{ } catch(Exception e){ } System.out.println("Thread1:"+citys[0]); Thread.sleep((int)(Math.random()*1000)); this.citys=citys; return time; time = System.currentTimeMillis() - start; System.out.println("Thread1 Ends!"); }
2
public void run(){ long start = System.currentTimeMillis(); for(int i=0;i<10;i++){ } try{ } catch(Exception e){ } System.out.println("Thread2:"+citys[1]); Thread.sleep((int)(Math.random()*1000)); time = System.currentTimeMillis() - start; System.out.println("Thread2 Ends!");
3