跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[问与答] Java 线程中如何实现休眠功能的线程安全

发表于

最近开始接手 Java 的项目,目前代码问题涉及到线程相关的内容,先说一下现状是
1 、线程是通过 new Thread().start 去启动的
2 、线程过一段时间就会假死
想咨询一下如何保证线程不假死,示例如下

public class thread1 implements Runnable {
	
    @Override
     public void run() {
     	while (true) {
        	try {
                if (condition) {
               	    // 代码
                    .......
                    
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        sout(e)
                    }
                } else {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        sout(e)
                    }
                }
            } catch (Exception e) {
            
            }
        }
    }

像这种一个线程逻辑里面,根据判断条件有多个休眠的,有什么好解决方案没,我查了一下 Timer 和 ScheduledExecutorService ,好像都不太符合,TimeUnit.时间单位.sleep 的底层好像也是 Thread.sleep 实现的,直接使用 ThreadPoolExecutor 去管理线程会不会有效,大家可以说说自己的看法

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.