java 多线程经常卡死的问题。



 public class b{
    public Long a(Long next_time){}
    public Long b(Long next_time){}
    public Long c(Long next_time){}
    public Long d(Long next_time){}
}

上面的B类中的a,b,c,d4个方法通过多线程方式同时执行。


 public class Main implements Runnable {

    public void run()
    {
        String method = Thread.currentThread().getName();
        Long next_time = 30000L;
        if(method.equals("a"))
        {
            while (true)
            {
                lottery b = new b();
                next_time = b.a();
                Thread.sleep(next_time);
            }
        } else if(method.equals("b"))
        {
            while (true)
            {
                lottery b = new b();
                next_time = b.b();
                Thread.sleep(next_time);
            }
        }else if(method.equals("c"))
        {
            while (true)
            {
                lottery b = new b();
                next_time = b.c();
                Thread.sleep(next_time);
            }
        }else if(method.equals("d"))
        {
            while (true)
            {
                lottery b = new b();
                next_time = b.d();
                Thread.sleep(next_time);
            }
        }
    }
    public static void main(String[] args) {
        new Thread(new Main(),"a").start();
        new Thread(new Main(),"b").start();
        new Thread(new Main(),"c").start();
        new Thread(new Main(),"d").start();
    }
}

上面的代码比较简单。就是并发无限执行b类里面的4个方法。通过数据库日志发现经常执行B类里面的某一个方法到某一行代码以后就不会往下面执行代码了。要通过重启jar才可以。要怎么设置超时或者其他方法一直无限运行。

java thread

少年特工喵组 10 years, 3 months ago

有可能是你的某个方法返回了较大的数值,所以一直在等待。

如果你想检查超时,可以另建一个线程,定时(按超时时间)去检查A-D线程的一个过得否执行完毕,如果未执行完毕,而且超过指定时间,则调用其 interrupt()

示意代码(仅示意,可能有点拼写或者语法错误)


 public class Main implements Runable {
    private long startTime;

    public long getStartTime() {
        return startTime;
    }

    public void run() {
        while (true) {
            try {
                startTime = System.currentTimeMillis();
                // .....
                Thread.sleep(next_time);
            } catch (InterruptedException e) {
                continue;
            }
        }
    }

    public static void main(String[] args) {
        Main a = new Main();
        new Thread(a, "a").start();
        DaemonThread daemon = new DaemonThread();
        daemon.setMain(a);
        new Thread(daemon, "daemon").start();
    }
}

class DaemonThread implements Runable {
    public static final long TIMEOUT = 30000L;

    // can use a list or array to instead
    Main a;

    // can use addMain to instead
    public void setMain(Main m) {
        a = m;
    }

    public void run() {
        while (true) {
            Thread.sleep(300)
            if (a == null) {
                continue;
            }

            if (System.currentTimeMillis() - a.getStartTime() > TIMEOUT) {
                a.interrupt();
            }
        }
    }
}

阿尔伯特威斯克 answered 10 years, 3 months ago

看着这种if .. else .. 蛋疼

2235760 answered 10 years, 3 months ago

最后可能是内存溢出了吧

大小姐的床单 answered 10 years, 3 months ago

Your Answer