Package com.ecyrd.speed4j

Source Code of com.ecyrd.speed4j.MultiThreadTest$TestRunnable

package com.ecyrd.speed4j;

import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.MalformedObjectNameException;
import javax.management.ReflectionException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.ecyrd.speed4j.log.PeriodicalLog;

public class MultiThreadTest
{
    StopWatchFactory swf;
    PeriodicalLog pl;
    @Before
    public void setUp()
    {
        pl = new PeriodicalLog();
       
        pl.setName( "multithread" );
        pl.setPeriod( 5 );
        pl.setSlf4jLogname( "multithread" );
        pl.setJmx( "Count" );
        pl.setSlowLogname( "slowmultithread" );
        pl.setSlowLogPercentile( 99 );
       
        swf = new StopWatchFactory( pl );
    }
   
    @After
    public void cleanUp()
    {
        pl.shutdown();
    }
   
    // This test fails if there's a ConcurrentModificationException or similar being thrown.
    @Test
    public void testMultiThread() throws MalformedObjectNameException, NullPointerException, IntrospectionException, InstanceNotFoundException, ReflectionException
    {
        int numThreads = 100;
        Thread[] threads = new Thread[numThreads];
       
        for( int i = 0; i < numThreads; i++ )
        {
            threads[i] = new Thread( new TestRunnable() );
            threads[i].start();
        }
       
        try
        {
            Thread.sleep(60*1000L);
        }
        catch( InterruptedException e ) {}
       
        // This should be okay.
        for( Thread t : threads ) t.stop();
    }
   
    private class TestRunnable implements Runnable
    {

        public void run()
        {
            while(true)
            {
                StopWatch sw = swf.getStopWatch();
                sw.setMessage("From thread "+Thread.currentThread().getName());
                @SuppressWarnings("unused")
                double d = 0.0;
                for( int i = 0; i < 1000; i++ )
                {
                    d += Math.random();
                }
//                try
//                {
//                    Thread.sleep(1000);
//                }
//                catch( InterruptedException e )
//                {
//                    e.printStackTrace();
//                }
                sw.stop("Count");
            }
        }
       
    }
}
TOP

Related Classes of com.ecyrd.speed4j.MultiThreadTest$TestRunnable

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.