Пример #1
0
  public static void main(String[] args) {
    if (SET_SINGLE_PROC_AFF) {
      setSolarisAffinity();
    }
    if (args.length == 1) {
      List<Thread> incThreads = new ArrayList<Thread>();
      int n;
      try {
        n = Integer.valueOf(args[0]);
        Filter lock = new Filter(n);
        Counter c = new Counter();
        AccessCounter acsCounter = new AccessCounter(n);

        for (int i = 0; i < n; i++) {
          Thread t = new IncThread(c, lock, i, acsCounter);
          incThreads.add(t);
        }

        long startTime = System.nanoTime();

        for (Thread thread : incThreads) {
          thread.start();
        }

        for (Thread thread : incThreads) {
          thread.join();
        }
        long endTime = System.nanoTime();

        System.out.println("Counter value = " + c.getVal());

        int[] acsCount = acsCounter.getCount();
        int totalCount = 0;
        for (int i = 0; i < n; i++) {
          int countVal = acsCount[i];
          totalCount += countVal;
          System.out.println("Counter accessed " + countVal + " by threadID " + i + "");
        }
        System.out.println("Total count accesses : " + totalCount);
        long totalTime = endTime - startTime;
        System.out.println("Execution time = " + totalTime + " nanoseconds");
      } catch (Exception e) {
        System.out.println("Please provide the number of threads as a numeric parameter.");
      }

    } else {
      System.out.println("Please provide the number of threads as a numeric parameter.");
    }
  }
Пример #2
0
 public void run() {
   boolean loopCondn = true;
   while (loopCondn) {
     lock.lock(threadID);
     try {
       if (myCounter.getVal() >= 300000) {
         loopCondn = false;
       } else {
         myCounter.increment();
         acsCounter.addAccessCount(threadID);
       }
     } finally {
       lock.unlock(threadID);
     }
   }
 }