死锁检测

   
public class threaddemo{    private static object lock1 = new object();    private static object lock2 = new object();    /**     * for test deadlock     *     * @param agrs     */    public static void main(string[] agrs)    {        new thread(new fightschedule1()).start();        executors.newscheduledthreadpool(1).scheduleatfixedrate(new fightschedule(), 0, 10, timeunit.seconds);        new thread(new fightschedule2()).start();    }    static class fightschedule2 implements runnable    {        public void run()        {            try            {                synchronized (lock2)                {                    thread.sleep(1000);                    synchronized (lock1)                    {                        thread.sleep(1000);                    }                }            }            catch (exception e)            {                e.printstacktrace();            }        }    }    static class fightschedule1 implements runnable    {        public void run()        {            try            {                synchronized (lock1)                {                    thread.sleep(1000);                    synchronized (lock2)                    {                        thread.sleep(1000);                    }                }            }            catch (exception e)            {                e.printstacktrace();            }        }    }    static class fightschedule implements runnable    {        public void run()        {            stringbuilder sb = new stringbuilder();            try            {                threadgroup group;                for (group = thread.currentthread().getthreadgroup(); group.getparent() != null; group =                    group.getparent())                    ;                sb.append("totalthread:" + group.activecount()).append('\n');                thread[] threads = new thread[group.activecount()];                group.enumerate(threads);                map<long, thread> threadmap = new hashmap<long, thread>();                for (thread thread : threads)                {                    if (thread != null)                    {                        threadmap.put(thread.getid(), thread);                    }                }                threadmxbean mxb = managementfactory.getthreadmxbean();                // 此方法设计用于故障排除,不用于异常控制。它是一个开销很大的操作。}
 

你可能感兴趣的:(java,工作)