欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    实验3java开发环境.docx

    • 资源ID:4677839       资源大小:195.75KB        全文页数:15页
    • 资源格式: DOCX        下载积分:3金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要3金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    实验3java开发环境.docx

    1、实验3java开发环境南京工程学院实验报告程序设计语言-JAVA开课院系:经济管理学院开课学期:2009-2010-2实 验:( 三 )班 级: K信管081学生姓名: 孟凡学 号:240083811 JAVA语言课程实验报告实验3:java开发环境一、实验目的及要求 熟悉JDK环境,掌握Jcreator的使用方法,理解Java 应用程序的运行原理和方法。二、实验设备(环境)及要求 JDK;Jcreator;Windows 操作系统三、实验内容与步骤实验内容:(一)、两个并发线程访问同一个对象object中的这个synchronized(this)同步代码块时,一个时间内只能有一个线程得到执行

    2、。另一个线程必须等待当前线程执行完这个代码块以后才能执行该代码块。更改下列程序,去掉synchronized(this),观察去掉前后的运行结果,并说明结果反映的问题。public class Thread1 implements Runnable public void run() synchronized(this) for (int i = 0; i 5; i+) System.out.println(Thread.currentThread().getName() + synchronized loop + i);public static void main(String args)

    3、 Thread1 t1 = new Thread1();Thread ta = new Thread(t1, A);Thread tb = new Thread(t1, B);ta.start();tb.start();(二):观察试验:回答问题。写出一组模拟生产者/消费者的协作程序其中包括一个WoTou.java,代表消息一个MsgQueue.java,为一个队列,提供put(Message msg)方法和get()方法一个Produer.java,为生产者线程,在其run方法中每隔1秒产生一个Message对像并放入MsgQueue队列一个Consumer.java为消费者线程,在其run

    4、方法中不断从MsgQueue队列中获取Message对像,并显示在屏幕上一个TestMain.java,在其main方法中,启动2个Produer线程和2个消费者线程.要求:对于MsgQueue.java,队列的长度为10,当消息超过10个时,put()方法需要阻塞:当消息队列为空时,get()方法需要阻塞。public class ProducerConsumer public static void main(String args) SyncStack ss = new SyncStack(); Producer p1= new Producer(ss,p1); Consumer c1=

    5、 new Consumer(ss,c1); Producer p2= new Producer(ss,p2); Consumer c2= new Consumer(ss,c2);class WoTou int id; WoTou(int id) this.id = id;public String toString() return WoTou : + id;class SyncStack int index = 0;WoTou arrWT = new WoTou6;public void push(WoTou wt) while(index = arrWT.length) try this.

    6、wait(); catch (InterruptedException e) e.printStackTrace(); this.notifyAll(); arrWTindex = wt; index +;public WoTou pop() while(index = 0) try this.wait(); catch (InterruptedException e) e.printStackTrace(); this.notifyAll(); index-; return arrWTindex;class Producer implements Runnable SyncStack ss

    7、= null;String a;Producer(SyncStack ss,String a) this.ss = ss; this.a= a; Thread t=new Thread(this,a); t.start();public void run() for(int i=0; i10; i+) synchronized(ss) WoTou wt = new WoTou(i); ss.push(wt); System.out.println(a+ 生产了: + wt); try Thread.sleep(int)(Math.random() * 200); catch (Interrup

    8、tedException e) e.printStackTrace(); class Consumer implements Runnable SyncStack ss = null;String a; Thread t;Consumer(SyncStack ss,String a) this.ss = ss; this.a= a; t=new Thread(this,a); t.start();public void run() for(int i=0; i10; i+) synchronized(ss) WoTou wt = ss.pop(); System.out.println(a+

    9、消费了: + wt); try Thread.sleep(int)(Math.random() * 1000); catch (InterruptedException e) e.printStackTrace(); p1 生产了:WoTou : 0p2 生产了:WoTou : 0p3 生产了:WoTou : 0c1 消费了: WoTou : 0c1 消费了: WoTou : 0p3 生产了:WoTou : 1c1 消费了: WoTou : 1c1 消费了: WoTou : 0p3 生产了:WoTou : 2p1 生产了:WoTou : 1c1 消费了: WoTou : 1p2 生产了:WoT

    10、ou : 1p2 生产了:WoTou : 2c1 消费了: WoTou : 2p2 生产了:WoTou : 3p3 生产了:WoTou : 3c1 消费了: WoTou : 3p3 生产了:WoTou : 4p2 生产了:WoTou : 4c1 消费了: WoTou : 4c1 消费了: WoTou : 4p1 生产了:WoTou : 2c1 消费了: WoTou : 2p2 生产了:WoTou : 5p3 生产了:WoTou : 5p2 生产了:WoTou : 6栈里有6个作为多线程交互一的一个重要应用,定义用户输入和输出的列队类或者堆栈类一定要看成一个整体放进同步语句块里面,不能简单的分别

    11、调用输入同步方法或者输出同步方法。下面的程序就有问题 下面这个是一个教程例子,但是结果不对。究其原因实际上是因为列队类或者堆栈类是一个可变类 ,这种可变类要是有参数传递给某些不可变类时,可变类的构造方法必须也设置成为同步方法,不然就会出错。public class ProducerConsumer public static void main(String args) SyncStack ss = new SyncStack(); Producer p = new Producer(ss); Consumer c = new Consumer(ss); new Thread(p).start

    12、(); new Thread(p).start(); new Thread(p).start(); new Thread(c).start();class WoTou int id; WoTou(int id) this.id = id;public String toString() return WoTou : + id;class SyncStack int index = 0;WoTou arrWT = new WoTou6;public synchronized void push(WoTou wt) while(index = arrWT.length) try this.wait

    13、(); catch (InterruptedException e) e.printStackTrace(); this.notifyAll(); arrWTindex = wt; index +;public synchronized WoTou pop() while(index = 0) try this.wait(); catch (InterruptedException e) e.printStackTrace(); this.notifyAll(); index-; return arrWTindex;class Producer implements Runnable Sync

    14、Stack ss = null;Producer(SyncStack ss) this.ss = ss;public void run() for(int i=0; i20; i+) WoTou wt = new WoTou(i); ss.push(wt);System.out.println(生产了: + wt); try Thread.sleep(int)(Math.random() * 200); catch (InterruptedException e) e.printStackTrace(); class Consumer implements Runnable SyncStack

    15、 ss = null;Consumer(SyncStack ss) this.ss = ss;public void run() for(int i=0; i20; i+) WoTou wt = ss.pop();System.out.println(消费了: + wt); try Thread.sleep(int)(Math.random() * 1000); catch (InterruptedException e) e.printStackTrace(); (三):编写一个继承thread类的类,然后实例化两个线程,并分别启动它们,并输出结果。步骤:打开java程序;编写代码;运行代码

    16、;修改保存四、实验结果(源程序)与数据处理(程序运行结果、截图等)1、代码显示结果截图更改下列程序,去掉synchronized(this),出现的报错,但能运行,截图如下:观察去掉前后的运行结果,并说明结果反映的问题2、实验截图结果原因分析:3、编写一个继承thread类的类,然后实例化两个线程,并分别启动它们,并输出结果。public class NumberThread extends Thread private int k; /序列初始值 public NumberThread(String name,int k) super(name); this.k = k; public Nu

    17、mberThread(String name) this(name,0); public void run() /覆盖run方法的线程体 int i = k; System.out.print(n+this.getName()+: ); while (i50) System.out.print(i+ ); i+=2; System.out.println(this.getName() +结束!); public static void main(String args) NumberThread thread_odd = new NumberThread(奇数线程,1); /创建线程对象 Nu

    18、mberThread thread_even = new NumberThread(偶数线程,2); /thread_odd.setPriority(10); /设置优先级为最高 thread_odd.start(); /启动线程对象 thread_even.start(); System.out.println(currentThread=+Thread.currentThread().getName(); /获得当前线程对象名 System.out.println(activeCount=+thread_even.activeCount(); System.out.println(main

    19、 Priority=+Thread.currentThread().getPriority(); /获得当前线程对象的优先级 /*程序运行结果如下:currentThread=mainactiveCount=3main Priority=5奇数线程: 1 3 5 7 9 11 13 15 17 19 21 23 25 27偶数线程: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 偶数线程结束!29 31 33 35 37 39 41 43 45 47 49 奇数线程结束!再次运行thread_odd.se

    20、tPriority(10); /最高优先级奇数线程: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 奇数线程结束!activeCount=2偶数线程: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 偶数线程结束!再次运行thread_odd.setPriority(1); /最低优先级activeCount=3偶数线程: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 偶数线程结束!奇数线程: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 奇数线程结束!*/截图如下:五、试验中遇到的问题、分析与讨论(手写)评定成绩:


    注意事项

    本文(实验3java开发环境.docx)为本站会员主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开