java实验-多媒体与多线程处理.docx
还剩
5页未读,
继续阅读
下载文档到电脑,马上远离加班熬夜!
亲,喜欢就下载吧,价低环保!
内容要点:
五、实验结果及分析通过这次实验,学会了 java 中多线程的应用,主要有 2 种方式实现,一种是子类化Thread,另一种是实现 Runnable 协议。try{blue.sleep(40);}catch(InterruptedException e){}}}}}四、实验过程原始数据记录1. 模拟小球平抛和自由落体的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); }}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;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)
发表评论
暂无评论,赶快抢占沙发吧。