thread - java 多线程经常卡死的问题。
阿神
阿神 2017-04-17 12:00:08
[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才可以。要怎么设置超时或者其他方法一直无限运行。

阿神
阿神

闭关修行中......

全部回复(3)
天蓬老师

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

伊谢尔伦

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

如果你想检查超时,可以另建一个线程,定时(按超时时间)去检查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();
            }
        }
    }
}
高洛峰

最后可能是内存溢出了吧

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号