/*
* 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 6173675
* @summary Basic test of ThreadMXBean.getThreadAllocatedBytes(long[])
* @author Paul Hohensee
*/
public class ThreadAllocatedMemoryArray {
private static boolean testFailed = false;
private static boolean done = false;
private static boolean done1 = false;
throws Exception {
if (!mbean.isThreadAllocatedMemorySupported()) {
return;
}
// start threads, wait for them to block
long[] ids = new long[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
}
// disable allocated memory measurement
if (mbean.isThreadAllocatedMemoryEnabled()) {
mbean.setThreadAllocatedMemoryEnabled(false);
}
if (mbean.isThreadAllocatedMemoryEnabled()) {
throw new RuntimeException(
"ThreadAllocatedMemory is expected to be disabled");
}
throw new RuntimeException("Null ThreadAllocatedBytes array returned");
}
for (int i = 0; i < NUM_THREADS; i++) {
long s = sizes[i];
if (s != -1) {
throw new RuntimeException(
"Invalid ThreadAllocatedBytes returned for thread " +
}
}
// Enable allocated memory measurement
if (!mbean.isThreadAllocatedMemoryEnabled()) {
}
if (!mbean.isThreadAllocatedMemoryEnabled()) {
throw new RuntimeException(
"ThreadAllocatedMemory is expected to be enabled");
}
for (int i = 0; i < NUM_THREADS; i++) {
long s = sizes[i];
if (s < 0) {
throw new RuntimeException(
"Invalid allocated bytes returned for thread " +
}
}
// let threads go and do some more allocation
synchronized (obj) {
done = true;
}
// wait for threads to get going again. we don't care if we
// catch them in mid-execution or if some of them haven't
// restarted after we're done sleeping.
goSleep(400);
for (int i = 0; i < NUM_THREADS; i++) {
throw new RuntimeException("TEST FAILED: " +
" previous allocated bytes = " + sizes[i] +
" > current allocated bytes = " + newSize);
}
" Previous allocated bytes = " + sizes[i] +
" Current allocated bytes = " + newSize);
}
try {
} catch (NullPointerException e) {
"Caught expected NullPointerException: " + e.getMessage());
}
try {
} catch (IllegalArgumentException e) {
"Caught expected IllegalArgumentException: " + e.getMessage());
}
// let threads exit
synchronized (obj) {
done1 = true;
}
for (int i = 0; i < NUM_THREADS; i++) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
if (testFailed) {
throw new RuntimeException("TEST FAILED");
}
}
try {
} catch (InterruptedException e) {
throw e;
}
}
private static void waitUntilThreadBlocked()
throws Exception {
int count = 0;
while (count != NUM_THREADS) {
goSleep(100);
count = 0;
for (int i = 0; i < NUM_THREADS; i++) {
count++;
}
}
}
}
public static void doit() {
long hashCode = 0;
}
" hashcode: " + hashCode);
}
super(name);
}
public void run() {
synchronized (obj) {
while (!done) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
}
synchronized (obj) {
while (!done1) {
try {
} catch (InterruptedException e) {
testFailed = true;
break;
}
}
}
}
}
}