2009-09-03 3 views
1

두 개의 동시 실행 스레드에 대한 쓰기 액세스 그래프를 그려야합니다. 스레드 자체에 간섭하지 않고 배열에 이러한 액세스의 타임 스탬프 값 쌍을 쓰는 가장 좋은 방법은 무엇입니까? 쓰여지는 대기열은 다음과 같습니다.Java에서 스레드 동작 분석

import java.util.concurrent.atomic.AtomicInteger; 

class IQueue<T> { 
    AtomicInteger head = new AtomicInteger(0); 
    AtomicInteger tail = new AtomicInteger(0); 
    T[] items = (T[]) new Object[100]; 

    public void enq(T x) { 
     int slot; 
     do { 
      slot = tail.get(); 
     } while (! tail.compareAndSet(slot, slot+1)); 
     items[slot] = x; 
    } 

    public T deq() throws EmptyException { 
     T value; 
     int slot; 
     do { 
      slot = head.get(); 
      value = items[slot]; 
      if (value == null) 
       throw new EmptyException(); 
     } while (! head.compareAndSet(slot, slot+1)); 
     return value; 
    } 

    public String toString() { 
     String s = ""; 
     for (int i = head.get(); i < tail.get(); i++) { 
      s += items[i].toString() + "; "; 
     } 
     return s; 
    } 
} 

쓰레드가 시작되거나 쓰일 때마다 기록하고 싶습니다.

+0

'LinkedBlockingQueue' /'ArrayBlockingQueue'를 사용하는 것이 잘못된 이유는 무엇입니까? – pjp

+0

btrace는 사전 컴파일하여 에이전트의 프로그램 시작 부분에 부착 할 수 있습니다. – VonC

+0

교육용입니다. – pypmannetjies

답변

1

실행중인 Java 프로그램의 동적 (바이트 코드) 계측 클래스에 대한 가능성은 BTrace입니다.
BTrace는 실행중인 Java 프로그램의 클래스에 추적 작업을 삽입하고 추적 된 프로그램 클래스를 핫 스왑합니다.

// import all BTrace annotations 
import com.sun.btrace.annotations.*; 
// import statics from BTraceUtils class 
import static com.sun.btrace.BTraceUtils.*; 

// @BTrace annotation tells that this is a BTrace program 
@BTrace 
public class HelloWorld { 

    // @OnMethod annotation tells where to probe. 
    // In this example, we are interested in entry 
    // into the Thread.start() method. 
    @OnMethod(
     clazz="java.lang.Thread", 
     method="start" 
    ) 
    public static void func() { 
     // println is defined in BTraceUtils 
     // you can only call the static methods of BTraceUtils 
     println("about to start a thread!"); 
    } 
} 
+0

그것은 매우 흥미 롭습니다. 그러나 자체적으로 자바 프로그램으로 실행해야하며 btrace는 명령 줄에서 실행해야합니다 ... 그래서 옵션이 아닙니다. :( – pypmannetjies