/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4892507
* @summary Basic Test for the following reset methods:
* - ThreadMXBean.resetPeakThreadCount()
* @author Mandy Chung
*
* @build ResetPeakThreadCount
* @build ThreadDump
*/
public class ResetPeakThreadCount {
// initial number of new threads started
// Terminate half of the threads started
// start new threads but expected the peak unchanged
// peak thread count reset before starting new threads
// barrier for threads communication
private static boolean testFailed = false;
// This test does not expect any threads to be created
// by the test harness after main() is invoked.
// The checkThreadCount() method is to produce more
// diagnostic information in case any unexpected test failure occur.
long current;
// start DAEMON_THREADS_1 number of threads
// terminate TERMINATE_1 number of threads and reset peak
// start DAEMON_THREADS_2 number of threads
// expected peak is unchanged
// Reset the peak
// start DAEMON_THREADS_3 number of threads
// terminate TERMINATE_2 number of threads and reset peak
if (testFailed)
throw new RuntimeException("TEST FAILED.");
}
// get current peak thread count
// Start threads and wait to be sure they all are alive
live[i] = true;
allThreads[i] = new MyThread(i);
allThreads[i].setDaemon(true);
allThreads[i].start();
}
// wait until all threads have started.
// get peak thread count after daemon threads have started
delta);
}
// wait until the current thread count gets incremented
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
}
return current;
}
// get current peak thread count
// Stop daemon threads and wait to be sure they all are dead
live[i] = false;
}
// wait until daemon threads terminated.
// get peak thread count after daemon threads have terminated
// assuming no system thread is added
" Expected to be = previous peak = " + peak1);
}
// wait until the current thread count gets decremented
try {
} catch (InterruptedException e) {
e.printStackTrace();
testFailed = true;
}
}
return current;
}
// Nightly testing showed some intermittent failure.
// Check here to get diagnostic information if some strange
// behavior occurs.
// Reset peak thread count
" current = " + current +
" after reset peak = " + afterResetPeak +
" current = " + afterResetCurrent);
if (afterResetPeak != current) {
throw new RuntimeException("Current Peak after reset = " +
" Expected to be = current count = " + current);
}
}
" previous = " + previous +
" current = " + current +
}
}
// The MyThread thread lives as long as correspondent live[i] value is true
int id;
}
public void run() {
// signal started
try {
sleep(100);
} catch (InterruptedException e) {
testFailed = true;
}
}
// signal about to exit
}
}
}