「java线程启动超时」多线程超时

博主:adminadmin 2022-11-22 13:33:09 64

今天给各位分享java线程启动超时的知识,其中也会对多线程超时进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 线程启动不了 求大神解决下谢谢

//修改了,添加了start、stop方法、并且在run方法中添加判断。。。

import java.awt.*;

import javax.swing.*;

public class test1 {

    public static void main(String[] args) {

        JFrame frm = new JFrame("Java Farme");

        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ball B1 = new ball(50, 50);

        frm.add(B1);

        B1.start();

        frm.setBounds(400, 200, 700, 400);

        frm.setVisible(true);

    }

}

class ball extends JPanel implements Runnable {

    int x;

    int y;

    Thread thread;

    public ball(int x, int y) {

        this.x = x;

        this.y = y;

    }

    public void paint(Graphics g) {

        super.paint(g);

        g.setColor(Color.blue);

        g.fillOval(x, y, 40, 40);

        g.dispose();

    }

    public void start() {

        if (null == thread) {

            thread = new Thread(this);

            thread.start();

        }

    }

    public synchronized void stop() {

        if (null != thread) {

            thread.interrupt();

            thread = null;

            notifyAll();

        }

    }

    public void run() {

        Thread me = Thread.currentThread();

        for (; me == thread;) {

            if (x  400) {

                x += 50;

                this.repaint();

            } else {

                x = 0;

                this.repaint();

            }

            try {

                Thread.sleep(100);

            } catch (InterruptedException ex) {}

        }

    }

}

java线程 如何监控用户操作超时

给一个我自己的丝路:

用户开始操作时,session记录状态。并开启定时器,延迟时间就是你允许用户操作的时间。定时器执行的代码,就是用户超时后,你要做的事情。

用户在时间范围内操作完成,肯定会给服务器发送完成信息,此时移除定时器即可。

java使用线程操作,等待线程后续时间过长报超时异常,如何操作使线程推出数据后即算线程操作结束

java中使用用线程控制Task任务,启动下面的线程就可以了,new Thread(new Task()).start() ;public class

Task implements Runnable {//新建一个任务

private TextArea textArea;

public Task(TextArea textArea){

this.textArea = textArea;

}

public void run() {

while (true) {

this.textArea.setText("这里设置: 输出的一段文字");

try {

Thread.sleep(500); // 这里设置:隔多长时间

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

java 线程池中正在运行的线程 如何设置超时时间 ps:如何设置一个线程最长的运行时间。

你可以设置一个计时器,然后把线程对象给它,让计时器在恰当时候把线程对象终止

Java线程超时管理

如果你是要在子线程超时后直接关闭的话可以这样:

exec=Executors.newFixedThreadPool(1);

CallableString call = new CallableString() {

public String call() throws Exception {

//开始操作

//TimeTaskTest.star(null);

return "线程执行完成";

}

};

FutureString future = exec.submit(call);

//设置为30分钟超时

future.get(1000*60*time , TimeUnit.MILLISECONDS);

=========================================

如果你是在进行其他操作的话那就比较麻烦了,目前我想到的是在子线程中一直访问某个变量,超时时守护线程改变这个值,然后子线程读到后退出,不过这个有个问题,就是当子线程卡死在某个环节时完全无用

关于Java线程超时退出的问题.

你在捕捉到的异常中,调用一下线程的interrupt()方法就可以对线程进行中断,当然,你的线程中的run方法中不能catch了interrupt()异常并且忽视这个异常。

比如在出现异常的地方:

threadA.interrupt();

而threadA的run方法中:

public void run() {

while(true) {

try {

....

} catch (InterruptedException) {

break; //这样是可以的,如果没有break或者修改为continue则不行

}

}

}

也可以将这个放在while循环之外。

public void run() {

try {

while(true) {

....

}

} catch (InterruptedException) {

....

}

}

java线程启动超时的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于多线程超时、java线程启动超时的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-22,除非注明,否则均为首码项目网原创文章,转载请注明出处。