下载此文档

java入门程序题及源码5.doc


文档分类:IT计算机 | 页数:约17页 举报非法文档有奖
1/17
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/17 下载此文档
文档列表 文档介绍
实验目的理解并掌握线程与同步学会使用java实现GUI设计学会使用awt和Swing组件的一般功能实验内容 实验题目一: 分别建立两个线程,产生并显示[100,10000]和[2000,3000]之间的随机数各 10000个,.*;classMyThread1implementsRunnable{publicvoidrun(){ Randomr=newRandom(); intn,i=0;longp=0;do{ n=(9901)+100; p=p+n; ("%d",n); i++; }while(i<10000); ();("thesumof10000numberbetween[100,10000]is:"+p);}} classMyThread2implementsRunnable{ publicvoidrun(){ Randomr=newRandom(); intn,i=0;longp=0;do{ n=(1001)+2000; p=p+n; ("%d",n); i++; }while(i<10000);();("thesumof10000numberbetween[2000,3000]is:"+p);}} lasstitle5_1{ publicstaticvoidmain(Stringargs[]){ ("therandomnumbersareshownasfollows:"); MyThread1holy=newMyThread1(); Threadt1=newThread(holy); (); MyThread2shit=newMyThread2(); Threadt2=newThread(shit); (); } }实验结果 实验题目二:建立两个线程分别不断读取两个文本文件中的数据并求和,当为-1时结束并显示求和结果。程序源代码 .*; .*; classsum{privateintsum=0;synchronizedvoidsum(inti){sum=sum+i; } publicvoidsum1(){ ("="+sum); }} classMyThread1implementsRunnable{ sumsum2; MyThread1(sumsum2){ =sum2; newThread(this).start(); }publicvoidrun(){try{Scannersc1=newScanner(newFile(""));intn;while(()){if((n=())==-1){ (); (-1); } else{ ("+%d",n); try{ (300); }catch(InterruptedExceptione){} (n); }}}catch(Exceptione){}}} classMyThread2implementsRunnable{ sumsum2; MyThread2(sumsum2){ =sum2; newThread(this).start(); }publicvoidrun(){try{Scannersc2=newScanner(newFile(""));intn;while(()){if((n=())==-1){ (); (-1); } else{ ("+%d",n); try{ (300); }catch(InterruptedExceptione){} (n); }}}catch(Exceptione){}}}lasstitle5_2{publicstaticvoidmain(String[]args){ ("sum=0"); sumsum1=newsum();MyThread1holy=newM

java入门程序题及源码5 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数17
  • 收藏数0 收藏
  • 顶次数0
  • 上传人在水一方
  • 文件大小238 KB
  • 时间2019-01-26
最近更新