网络编程技术次课多线程代码

上传人:公**** 文档编号:565013507 上传时间:2024-02-08 格式:DOCX 页数:9 大小:27.51KB
返回 下载 相关 举报
网络编程技术次课多线程代码_第1页
第1页 / 共9页
网络编程技术次课多线程代码_第2页
第2页 / 共9页
网络编程技术次课多线程代码_第3页
第3页 / 共9页
网络编程技术次课多线程代码_第4页
第4页 / 共9页
网络编程技术次课多线程代码_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《网络编程技术次课多线程代码》由会员分享,可在线阅读,更多相关《网络编程技术次课多线程代码(9页珍藏版)》请在金锄头文库上搜索。

1、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 Le

2、fthand 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 pu

3、blic 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的控制权进入中断状态矚慫润厲钐瘗睞枥庑赖。*/ pub

4、lic 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 set

5、Sum(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;/设置休眠时间 puterSum = sum; /设置ComputerSum类的对象sum

6、public 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); /线程

7、休眠 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 Peopl

8、e1(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, stu

9、dent;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 compute

10、r(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

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号