使用可运行的单例闪烁文本

时间:2016-09-09 11:24:09

标签: java singleton runnable

我有一些组件句柄View。我可以像这样改变标签的颜色。
labelHandle.setForeground(Color.Blue);

我想让这个label的文字闪烁 - 我的想法是View我会有方法blink(Boolean on),如果我调用blink(true),方法将启动新线程将是实施Runnable的课程。

run()的循环将是这样的

run() {
    for(;;) {
        if(finished == true) break;
        Thread.sleep(300);
        //this will do the blinking
        if(finished == true) break; //if durring sleep finish was called => end
        if(change) labelHandle.setForeground(Color.Blue);
        else labelHandle.setForeground(Color.Black);

        change = (change) ? false : true; //switch the condition 
    }
}

有一些要点:

boolean finished - says if the thread is finished, the loop for bliniking 
void fihish() {this.finished = false;} //stops the thread job 
boolean change - switch for the collors, each end of loop switch this value, if its true set one color, if its false sets another => here we have blinking ... 

直到这里它非常清楚如何做到这一点。主要思想是制作线程,它将带来眨眼。
在视图中我会打电话:

blink(true);
blink(false);

它会使星星停止并停止。
有一次只有一个视图可以激活,所以我想只有一个线程可以闪烁,可以用于更多视图。该线程将是实现Runnable的类,并且应该是Singleton以确保只有一个线程。
这样做Singleton线程是个好主意吗? (好的或坏的做法)

2 个答案:

答案 0 :(得分:0)

您应该使用ScheduledExecutorService创建的Executors.newSingleThreadScheduledExecutor()(实际上是可用于计划任务的线程池),而不是尝试重新发明轮子(这只是一个池一个线程)每隔300毫秒安排一次任务,然后在你想要停止任务时在相应的cancel(boolean)实例上调用Future

这样的事情:

// Create the scheduler that will use only one thread to execute its tasks
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
// Schedule the task every 300 milliseconds starting in 300 milliseconds
ScheduledFuture<?> future = executor.scheduleAtFixedRate(
    new Runnable() {
        @Override
        public void run() {
            if(change) labelHandle.setForeground(Color.Blue);
            else labelHandle.setForeground(Color.Black);
            change = !change; //switch the condition
        }
    }, 300L, 300L, TimeUnit.MILLISECONDS
);
...
// Stop it when it is done
future.cancel(false);

您的executor可以定义为private static final字段,因为您只需要一个调度程序,这样您的整个应用程序中只有一个线程。

答案 1 :(得分:0)

这是一个启动/停止单件运行的选项

    public class Blink implements Runnable {

    private static Blink blink;
    private static Thread thread;
    private static boolean stop = false;

    @Override
    public void run() {

        while (! stop ) {

            System.out.println("running ");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) { ex.printStackTrace();}
        }


        System.out.println("stopped " );
    }

    public static void doBlink(boolean start) {

        if(blink == null) {

            blink= new Blink();
        }

        if(start) {

            stop = true; //stop thread if running
            thread = new Thread(blink);
            thread.start();
            stop = false;

        }else {

             stop = true;
        }

    }

    public static void main(String[] args) {

        Blink.doBlink(true);

        try {
            Thread.sleep(4000);
        } catch (InterruptedException ex) { ex.printStackTrace();}

        Blink.doBlink(false);
    }
}
相关问题