KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > jvm > HeapThread


1 /*
2  * 00/08/01 @(#)HeapThread.java 1.3 Copyright (c) 2000 Sun Microsystems, Inc.
3  * All Rights Reserved. Sun grants you ("Licensee") a non-exclusive, royalty
4  * free, license to use, modify and redistribute this software in source and
5  * binary code form, provided that i) this copyright notice and license appear
6  * on all copies of the software; and ii) Licensee does not utilize the software
7  * in a manner which is disparaging to Sun.
8  */

9
10 package org.objectweb.cjdbc.scenario.standalone.jvm;
11
12 public class HeapThread extends Thread JavaDoc
13 {
14
15   private int m_id = 0;
16   private int m_numThreads = 0;
17   private int[] m_data = null;
18   private int m_numDataPoints = 0;
19   private int m_numIterations = 0;
20   private int m_numNodesToAlloc = 0;
21   private int m_heapCycles = 0;
22   private int m_cpuCycles = 0;
23   private Barrier m_goFlag = null;
24   private Node m_firstNode = null;
25
26   public HeapThread(int id, int numThreads, int[] data, int numDataPoints,
27       int numIterations, int numNodesToAlloc, int heapCycles, int cpuCycles,
28       Barrier goFlag)
29   {
30     m_id = id;
31     m_numThreads = numThreads;
32     m_data = data;
33     m_numDataPoints = numDataPoints;
34     m_numIterations = numIterations;
35     m_numNodesToAlloc = numNodesToAlloc;
36     m_heapCycles = heapCycles;
37     m_cpuCycles = cpuCycles;
38     m_goFlag = goFlag;
39     m_firstNode = new Node();
40   }
41
42   /**
43    * @see java.lang.Runnable#run()
44    */

45   public void run()
46   {
47
48     // Wait for all the threads are ready to go
49
m_goFlag.waitForGo();
50
51     for (int iter = m_id; iter < m_numIterations; iter += m_numThreads)
52     {
53       for (int heapIter = 0; heapIter < m_heapCycles; heapIter++)
54       {
55         doHeapBoundStuff(m_numNodesToAlloc);
56       }
57       for (int cpuIter = 0; cpuIter < m_cpuCycles; cpuIter++)
58       {
59         doCPUBoundStuff(m_data, m_numDataPoints);
60       }
61     }
62   }
63
64   void doHeapBoundStuff(int numNodesToAlloc)
65   {
66     if (m_firstNode.m_next == null)
67     {
68       for (Node node = m_firstNode; numNodesToAlloc > 0; numNodesToAlloc--)
69       {
70         node.m_next = new Node();
71         node = node.m_next;
72       }
73     }
74     else
75     {
76       while (m_firstNode.m_next != null)
77       {
78         m_firstNode.m_next = m_firstNode.m_next.m_next;
79       }
80     }
81   }
82
83   /*
84    * double sqrtByNewtonsMethod(double x) { double lastEst = x; double est = 0;
85    * double epsilon = 1e-10; if (x == 0) est = 0; else { for (;;) { est =
86    * (lastEst*lastEst + x)/(2*lastEst); if (-epsilon < (est-lastEst) && (est -
87    * lastEst) < epsilon) { break; } else lastEst = est; } } return est; }
88    */

89
90   void doCPUBoundStuff(int[] data, int numDataPoints)
91   {
92     int i = 0;
93     double avg = 0;
94     double var = 0;
95
96     data[0] = m_id;
97     data[1] = m_id + 1;
98
99     for (i = 2; i < numDataPoints; i++)
100     {
101       data[i] = data[i - 1] + data[i - 2];
102     }
103
104     for (avg = 0, i = 0; i < numDataPoints; i++)
105     {
106       avg += numDataPoints;
107     }
108     avg /= numDataPoints;
109
110     for (var = 0, i = 0; i < numDataPoints; i++)
111     {
112       double diff = data[i] - avg;
113       var += diff * diff;
114     }
115     var /= (numDataPoints - 1);
116   }
117 }
118
119
Popular Tags