java实验14 多线程1 - 答案.doc_第1页
java实验14 多线程1 - 答案.doc_第2页
java实验14 多线程1 - 答案.doc_第3页
java实验14 多线程1 - 答案.doc_第4页
java实验14 多线程1 - 答案.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验十四 多线程(一)一、实验时间: 姓名: 学号: 二、 实验目的1、 了解线程的概念;2、 掌握通过Thread类创建线程;3、 掌握通过Runnable接口创建多线程;4、 掌握多线程的同步;三、知识点1、线程的概念;2、通过Thread类创建线程;3、通过Runnable接口创建多线程;4、同步函数四、实验内容与步骤1、请思考并举例说明为何需要多线程,实现多线程的好处是什么?2、编程实现以下功能:通过主线程控制另外两个线程,这两个线程分别在命令行窗口的左侧和右侧顺序地、一行一行地输出字符串。主线程负责判断输出的行数,当其中任何一个线程输出8行后就结束进程。要求:分别使用Thread的子类和Runable接口创建线程。1)使用Thread的子类完成/主线程程序public class Example8_3 public static void main(String args) Left left=new Left();Right right=new Right();left.start();right.start();while(true)if(left.n=8|right.n=8)System.exit(0);/左边线程public class Left extends Thread int n=0;public void run()while(true)n+;System.out.printf(n%s, 我在左面写字);trysleep(int)(Math.random()*100)+100);catch(InterruptedException e)/右边线程public class Right extends Thread int n=0;public void run()while(true)n+;System.out.printf(n%40s, 我在右面写字);trysleep(int)(Math.random()*100)+100);catch(InterruptedException e)2)Runable接口创建线程/主线程程序public class Example8_4 public static void main(String args) Left1 l1=new Left1();new Thread(l1).start();Right1 r1=new Right1();new Thread(r1).start();while(true)if(l1.n=8|r1.n=8)System.exit(0);/左边线程public class Left1 implements Runnable int n=0;Overridepublic void run() / TODO Auto-generated method stubwhile(true)n+;System.out.printf(n%s, 我在左面写字);tryThread.sleep(int)(Math.random()*100)+100);catch(InterruptedException e)/右边线程public class Right1 implements Runnableint n=0;Overridepublic void run() / TODO Auto-generated method stubwhile(true)n+;System.out.printf(n%40s, 我在右面写字);tryThread.sleep(int)(Math.random()*100)+100);catch(InterruptedException e)3、编程模拟四个售票进程共同出售100张火车票,要保证线程安全。public class ThreadTest implements Runnable private int tickets=100;Overridepublic void run() while(true)sale();public synchronized void sale()if(tickets0)System.out.println(Thread.currentThread().getName()+ is saling ticket +tickets-);public class ThreadDemo public static void main(String args) / TODO Auto-generated method stubeThreadTest t=new ThreadTest();new Thread(t).start();new Thread(t).start();new Thread(t).start();new Thread(t).start();4、创建两个线程:accountant和cashier,它们共同拥有一个账本。它们都可以使用saveOrTake(int number)对账本进行访问,会计使用saveOrTake()方法时,向账本上写入存钱记录;出纳使用saveOrTake()方法时,向账本写入取钱记录。因此,当会计正在使用saveOrTake()方法时,出纳被禁止使用,反之也是这样。比如,会计每次使用saveOrTake()方法时,在账本上存入90万元,在存入这笔钱时,分3次存完,每存入30万元,就休息一会儿,休息时出纳仍不能使用saveOrTake()方法。要保证其中一人使用saveOrTake()方法时,另一个人将必须等待。/Bank.javapublic class Bank implements Runnable int money=300;String accountantName,cashierName;public Bank(String s1,String s2)accountantName=s1;cashierName=s2;public void run() / TODO Auto-generated method stubsaveOrTake(30);public synchronized void saveOrTake(int number)if(Thread.currentThread().getName().equals(accountantName)for(int i=1;i=3;i+)money=money+number;tryThread.sleep(1000);catch(InterruptedException e)System.out.println(我是:+accountantName+目前账上有+money+万);elsefor(int i=1;i=2;i+)money=money-number/2;tryThread.sleep(1000);catch(InterruptedException e)System.out.println(我是:+cashierName+目前账上有+money+万);/public class Example8_11 public static void main(String args) String accountantName=会计,cashierName=出纳;Bank bank=new Bank(accountantName,cashierName);Thread accountant,cashier;accountant=new

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论