增加线程的速度 [英] Increacing the speed of thread

查看:128
本文介绍了增加线程的速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次调用线程后,它的速度都会增加(具体来说 - FirstCircleRepaintThread,SecoundCircleRepaintThread,LineRepaintThread)。但主线程的速度是正常的。我已经阅读了这个问题,但我的帖子速度总是增加。
GitHub Dropbox上的JAR
PS抱歉,我的英文不好

After every call of thread it's speed is increased (specifically - FirstCircleRepaintThread, SecoundCircleRepaintThread, LineRepaintThread). But the main thread's speed is normal. I've read this question, but my thread's speed is always increasing. GitHub, JAR on Dropbox P.S. Sorry for my bad english

推荐答案

问题是,每次取消阻止你正在创建一个新的线程实例,但是你没有停止它们,所以它们仍在运行,更新你的UI

The problem is, each time you unblock you are creating a new instance of your threads, but you're not stopping the old ones, so they are still running, updating your UI

public void block() {
    setVisible(true);

    // Create a bunch of new threads...        
    firstCircleThr = new FirstCircleRepaintThread();
    firstCircleThr.setPriority(Thread.MAX_PRIORITY);
    firstCircleThr.start();
    secoundCircleThr = new SecoundCircleRepaintThread();
    secoundCircleThr.setPriority(Thread.MAX_PRIORITY);
    secoundCircleThr.start();
    lineThr = new LineRepaintThread();
    lineThr.setPriority(Thread.MAX_PRIORITY);
    lineThr.start();
}

public void unblock(){

    setVisible(false);
    // Dereference the old ones, but they are still running...        
    firstCircleThr = null;
    secoundCircleThr = null;
    lineThr = null;
    System.out.println("Yeah! OFF IT!");
}

例如......

public class FirstCircleRepaintThread extends Thread{


    public static final long SPEED_OF_ROTATING = 25L;

    @Override
    public void run(){
        //while(true){

            MainCycle.frame.panel.startAngleFirst = 34;
            int i = 0;

            Random r = new Random();

            // To infinity and beyond...                
            while(true){

你需要提供一些方法来阻止这些线程...不用调用停止 ...

You need to supply some way you can stop these threads...without calling stop...

例如......

public class FirstCircleRepaintThread extends Thread{

    private volatile boolean keepRunning = true;

    public static final long SPEED_OF_ROTATING = 25L;

    public void kull() {

        keepRunning = false;
        interrupt();
        try {
            join();
        } catch (InterruptedException ex) {
        }

    }

    @Override
    public void run(){
        //while(true){

            MainCycle.frame.panel.startAngleFirst = 34;
            int i = 0;
            Random r = new Random();

            while(keepRunning){

现在你 block 方法,你应该调用 firstCircleThr.kull(),这将停止 Thread 返回之前...

Now in you block method, you should be calling firstCircleThr.kull(), which will stop the Thread before returning...

如果您使用调试器或在循环之间看到框架可见,您可能已经看过这个...

You could have seen this if you used a debugger or left the frame visible between cycles...

现在,说了这么多,你违反了单线程规则Swing,更新了除了事件调度线程之外的每个线程的UI状态。

Now, having said all that, you're violating the single thread rules Swing, updating the state of the UI from every thread except the Event Dispatching Thread.

查看 Swing中的并发并考虑使用 SwingWorker Swing 计时器

Take a look at Concurrency in Swing and consider the use of a SwingWorker or Swing Timer.

您应该考虑保持少量高建群尽可能地处理流程以保持性能并降低更改模型和交互的复杂性

You should consider maintaining as few background processes as possible in order to maintain performance and reduce the complexity of changing the model and interafce

这篇关于增加线程的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆