·¢²¼Ê±¼ä : ÐÇÆÚ¶þ ÎÄÕÂÆßÖÖ·½·¨ÇóË®ÏÉ»¨Êý¸üÐÂÍê±Ï¿ªÊ¼ÔĶÁ
______ Ë®ÏÉ»¨Êý
1.4.3 Óöµ½µÄÎÊÌâ¼°½â¾ö·½°¸ ºó¹û
·ÖÎö
»·¾³ÅäÖôíÎó£¬ÔÚÅäÖÃMPIµÄ»·¾³Ê±»¨Á˺ܶàʱ¼ä£¬ÔÚ×Ô¼ºµçÄÔÉÏÔËÐÐÒ»Ö±³ö´í£¬ºóÀ´ÔÚ»ú·¿ÔËÐгɹ¦µÄ¡£
1.5 »ùÓÚJava£¨Runnable£©µÄ²¢ÐÐË㷨ʵÏÖ 1.5.1 ´úÂë¼°×¢ÊÍ£¨±äÁ¿Ãû Ãû×ÖÊ××Öĸ ¿ªÍ·£©
import java.lang.Thread;
public class JavaThread extends Thread {
private int start; private int end;
______ Ë®ÏÉ»¨Êý
private int sum=0;
public JavaThread(int start,int end) {
super();
this.start=start; this.end=end; }
public void run()//²¢Ðк¯Êý {
int a,b,c;
for(int xlh=start;xlh<=end;xlh+=2) {
a=xlh/100; b=(xlh-a*100)/10; c=xlh; try { sleep(1);
} catch (InterruptedException e) { e.printStackTrace(); }
if(a*a*a+b*b*b+c*c*c==xlh&&xlh>100&&xlh<1000) { sum++;
System.out.println(xlh);
______ Ë®ÏÉ»¨Êý
} } }
public int Flower() throws InterruptedException//´®Ðк¯Êý {
int a,b,c; sum=0;
for(int xlh=start;xlh<=end;xlh++)//ÇóË®ÏÉ»¨Êý {
a=xlh/100; b=(xlh-a*100)/10; c=xlh; sleep(1);
if(a*a*a+b*b*b+c*c*c==xlh&&xlh>100&&xlh<1000) { sum++;
System.out.println(xlh); } }
return sum; }
public int getSum() {
______ Ë®ÏÉ»¨Êý
return sum; }
public static void main(String []args)throwsInterruptedException {
JavaThread thread1=new JavaThread(1,1000);//È·¶¨¿ªÊ¼ÒÔ¼°½áÊøÖµ JavaThread thread2=new JavaThread(2,1000); System.out.println(\²¢Ðнá¹û:\);
long startTime=System.currentTimeMillis();//²¢ÐпªÊ¼ thread1.start(); thread2.start(); thread1.join(); thread2.join();
long endTime=System.currentTimeMillis();
System.out.println(\²¢ÐÐʱ¼ä:\+(endTime-startTime)); double t1=endTime-startTime;
startTime=System.currentTimeMillis();//²¢ÐнáÊø
System.out.println(\´®Ðнá¹û:\);
JavaThread thread=new JavaThread(1,1000); thread.Flower();
endTime=System.currentTimeMillis();
System.out.println(\´®ÐÐʱ¼ä:\+(endTime-startTime)); double t2=endTime-startTime; System.out.println(\¼ÓËÙ±È\+t2/t1);