资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
1) 第一个程序:LeftAndRight.javapublic class LeftAndRight public static void main(String args) Lef thand lef t;/声明线程类对象Righthand right;left = new Lefthand();/ 新建线程。right = new Righthand();left.start(); /线程开始运行后,Lefthand类中的run方法将被执行。 right.start();for ( int i = 0; i = 6; i+) System. out. println();class Lefthand extends Thread public void run() for ( int i = 1; i = 9; i+)System. out .println(我是左手线程);try /线程执行一次输出后,休眠500ms (毫秒),使自己放弃CPU资源,/500ms时间到后,重新排队等待CPU的调度 sleep(500); catch (InterruptedException e) /如果线程在休眠期被打断,将抛出InterruptedException,/所以sleep方法需要在trycatch语句块中被调用class Righthand extends Thread public void run() for ( int i = 1; i = 5; i+)System. out. println();try sleep(300); catch (InterruptedException e) 2) 第二个程序:StudentAndTeacher javapublic class StudentAndTeacher /*两个线程共享一个对象ComputerSum sum,在运行的过程中修改这个对象的成员变量sum。*为了使结果尽量不依赖于当前CPU资源的使用情况,应当让线程主动调用sleep方法, 让出CPU的控制权进入中断状态矚慫润厲钐瘗睞枥庑赖。*/ public static void main(String args) / TODO Auto-generated method stub ComputerSum sum = new ComputerSum();People teacher = new People(教师, 100, sum);People student = new People(学生,100, sum); teacher.start();student.start(); class ComputerSum /线程共享的对象类ComputerSum,其中定义了一个数据成员sum int sum=0;public void setSum(int n) sum = n;public int getSum() return sum; class People extends Thread /类People是Thread的子类ComputerSum computerSum;int timeLength;People(String s, int timeLength, ComputerSum sum) /构造方法聞創沟燴鐺setName(s);/ 设置Thread的名称 this .ti meLeng th = ti meLeng th;/设置休眠时间 this.computerSum = sum; /设置ComputerSum类的对象sumpublic void run() /重写Thread父类的run ()方法 for (int i = 1; i = 5; i+)int m = comp ut erSum.ge tSum();/取出 comp ut erSum 对象中的数据成员 sum残骛楼諍锩瀨濟溆塹籟。computerSum.setSum(m+1); /将数据成员sum的值加1System. out . println(我是+ getName() + ,现在的和是+ computerSum.getSum(); 酽锕极額閉镇桧猪訣锥。/输出当前处于运行中的线程的信息try sleep (ti meLeng th); /线程休眠 ti meLeng th 毫秒 catch (InterruptedException e) 3) 第三个程序:StudentAndTeacherl javapublic class StudentAndTeacher1 /* Stu den tA ndTeacherl 为Stu den tA ndTeacher 的改进版本*/public static void main(String args) / TODO Auto-generated method stubComputerSum sum = new ComputerSum();People1 people = new People1(200, sum);people.teacher.start();people.student.start();class ComputerSum1 /线程共享的对象类ComputerSum,其中定义了一个数据成员sumint sum = 0;public void setSum(int n) sum = n;public int getSum() return sum;class People1 implements Runnable /类People是Thread的子类ComputerSum computerSum;int timeLength;Thread teacher, student;People1(int timeLength, ComputerSum sum) / 构造方法t eacher = new Thread( this);/类 People1 实现了接口 Runnable teacher.setName(教师);student = new Thread( this);student .setName(学生);this.timeLength = timeLength; / 设置休眠时间this . comp ut erSum = sum;/ 设置 Comp ut erSum 类的对象 sumpublic synchronized void computer(Thread thread) int m = comp ut erSum .ge tSum();/ 取出 comp ut erSum 对象中的数据成员 sumcomputerSum.setSum(m + 1);/ 将数据成员sum的值加1System. out . println(我是+ thread.getName() + ,现在的和是+ computerSum.getSum();public void run() /重写Thread父类的run ()方法 for (int i = 1; i realNumber) /猜得的数偏大 message = LARGER;System.out. println(); else if (guessNumber realNumber) /猜得的数偏小 message = SMALLER;System.out. println(); else if (guessNumber = realNumber) /猜对了, guessNumber = realNumber messa
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号