2016-10-21 2 views
0

을 사용했지만 방금 병렬 프로그래밍을 시작했습니다. 그러나 병렬로 작업을 수행하는 데 걸리는 시간을 계산하는 작업이있었습니다. 이제는 이미 "System.currentTimeMillis();"인스턴스를 사용해야한다는 것을 이미 알고 있습니다. 이 작업을 보장하기 위해 작업을 수행하기 전에 구현해야 할 것입니다. 그리고 이후에 작업을 초기화하는 데 걸린 시간을 결정해야합니다. 이것은 내가 이것을 구현하려고 할 때마다 똑같은 타이밍을 얻고 올바르게 구현한다고 생각하지 않기 때문에 크게 혼란 스럽습니다. 자바로 '클릭'을 얻을 수없는 것처럼 어떤 도움이라도 대단히 감사하겠습니다.도움을 주셔서 감사합니다 'System.currentTimeMillis();' 내 대학 과정에 병렬 Doubler 프로그램

미안 포맷되지 않은 경우 일을 올바르게 등

여기

내 주요 클래스 : 여기

import java.util.*; 
import java.lang.*; 

public class Main { 

public static void main(String[] args) { 



    // Define a new array 
    ArrayList<Integer> L = new ArrayList<Integer>(100); 

    // Initialise the array with random values between 0 and 100 
    Random R = new Random(); 

    for (Integer i=0; i<100; i++) { 

     L.add(R.nextInt(10)); 


    } 

    for (Integer i=0; i<100; i++) { 


     System.out.print(L.get(i)); 
     System.out.print(", "); 

    } 

    System.out.println(""); 

    // Process the array 
    Thread doubler1 = new Doubler(L, 0, 50); 
    Thread doubler2 = new Doubler(L, 50, 100); 

    doubler1.start(); 
    doubler2.start(); 

    try { 
     doubler1.join(); 
     doubler2.join(); 
    } 
    catch(InterruptedException e) {} 


    for (Integer i=0; i<100; i++) { 

     System.out.print(L.get(i)); 
     System.out.print(", "); 

    } 

} 

} 

그리고 내 배율기 클래스입니다 :

import java.util.*; 
import java.lang.*; 



public class Doubler extends Thread { 

// We'll store a reference to the list we want to process 
// (all instances could see the same list) 
private ArrayList<Integer> L; 

// Store the range within the array we want to process 
private int startIndex, endIndex; 

// Constructor to initialise Doubler instance - this stores a reference to the list to process 
// and the start and end indices to process when the thread is run. 
public Doubler(ArrayList<Integer> list, int start, int end) { 

    L = list; 


    startIndex = start; 

    endIndex = end; 

} 

// Run this on a thread - managed by the JRE 
public void run() { 

    // Process the range of integers by doubling each value 
    for (int i=startIndex;i<endIndex;i++) { 

     L.set(i, L.get(i) *2); 
    } 
} 

} 
+0

'System.currentTimeMillis()'를 사용하여 경과 시간을 측정하지 마십시오. http://stackoverflow.com/questions/1770010/how-do-i-measure-time-elapsed-in-java를 참조하십시오. –

+0

직접 시도한 코드를 추가하고 ** 왜 ** 귀하의 결과가 정확하지 않다고 생각하는지 설명하십시오. – Tom

+0

) System.currentTimeMillis()를 사용하라는 지시를 받았습니다.) 시스템 클럭이 약간 꺼져 있으면 문제가되지 않습니다. – R0ckTillWeDr0p

답변

0

System.currentTimeMillis()는 반환 할 수 있습니다 두 번 전화가 너무 가까워지면 같은 시간. 이런 식으로 시간을 측정해야하므로 시간을 정확하게 측정 할 수 있도록 스레드를 변경하여 더 긴 작업을 수행해야합니다.

관련 문제