实验目的理解并掌握线程与同步学会使用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转载请标明出处.