可以单独访问的多个线程

发布于 2024-11-09 13:36:50 字数 949 浏览 0 评论 0原文

我有一个按钮,按下该按钮后,应该创建一个新线程的实例:倒计时器。问题是,我需要使用某种动态数组来执行此操作,因为无法知道用户将按下按钮多少次!

这是按钮的操作侦听器中的代码:

 Counter c = new Counter(timeToFinish);

这是 Counter 类的代码:

class Counter implements Runnable {

        int waitingTime = 0;
        Thread myCounter = new Thread(this);

        public Counter(int waitingTime)
        {
            this.waitingTime = waitingTime;
            myCounter.start();
        }

        public void run(){

            //Start countdown:
            do  
            {

                waitingTime -= 1;

                try {

                    Thread.sleep(1000);
                    System.out.println(waitingTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (waitingTime >= 0);

        }
    }

如果用户按按钮十次,则应创建从 c(0) 到 c(9) 的十个实例,每个实例都单独的线程。我不知道如何创建动态线程数组:S

I have a button, that when pressed, should create an instance of a new thread: a Countdown Timer. The problem is, I need to do this with some kind of dynamic array, because there is no way of knowing how many times the user will press the button!

This is the code in the button's action listener:

 Counter c = new Counter(timeToFinish);

This is the code for the Counter class:

class Counter implements Runnable {

        int waitingTime = 0;
        Thread myCounter = new Thread(this);

        public Counter(int waitingTime)
        {
            this.waitingTime = waitingTime;
            myCounter.start();
        }

        public void run(){

            //Start countdown:
            do  
            {

                waitingTime -= 1;

                try {

                    Thread.sleep(1000);
                    System.out.println(waitingTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } while (waitingTime >= 0);

        }
    }

If the user presses the button ten times, ten instances should be created, from c(0) to c(9), each separate threads. I don't know how to create a dynamic array of threads :S

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的笑 2024-11-16 13:36:51

我需要使用某种动态数组来做到这一点

尝试使用ArrayList

如果用户按按钮十次,则应创建十个实例,从 c(0) 到 c(9),每个实例单独的线程。我不知道如何创建动态线程数组:S

类似这样的事情应该做:

  1. 创建一个List来存储计数器:

    列表<计数器> myCounters = new ArrayList();
    
  2. 添加新的计数器线程,如下所示:

    int nexti = myCounters.size();
    myCounters.add(new Counter(nexti));
    

I need to do this with some kind of dynamic array

Try using an ArrayList.

If the user presses the button ten times, ten instances should be created, from c(0) to c(9), each separate threads. I don't know how to create a dynamic array of threads :S

Something like this should do:

  1. Create a List to store the counters:

    List<Counter> myCounters = new ArrayList<Counter>();
    
  2. Add new counter-threads like this:

    int nexti = myCounters.size();
    myCounters.add(new Counter(nexti));
    
刘备忘录 2024-11-16 13:36:51

创建一个存储列表,

List<Counter> lst = new ArrayList<Counter>();

将线程添加到列表中,单击“

Counter counter = new Counter(someInt);
lst.add(counter );

同时尝试管理从列表中删除线程的引用”

Create a storage list

List<Counter> lst = new ArrayList<Counter>();

add thread to list, on click

Counter counter = new Counter(someInt);
lst.add(counter );

Also try to manage deleting reference of thread from List

拔了角的鹿 2024-11-16 13:36:51

我绝对不会在每次用户单击按钮时创建一个新线程。如果他们点击它几百次,您的应用程序可能会死掉或变慢。当他们单击按钮时,您知道您希望计时器何时到期。因此,创建一个 Timer 对象并将其放入有序队列(从最早到最晚到期时间排序),然后使用单个线程监视该队列,并在 Timer 到期时将其弹出。

然后,您的 TimerWatcher 线程可以每隔几毫秒唤醒一次,或者更好的是,等到它知道第一个计时器即将到期,然后在下一个计时器达到峰值并等待下一个计时器到期。每当您添加新的计时器时,您还可以唤醒您的 TimerWatcher 并检查新的计时器是否尚未到期,以确保它不会在您已经等待的计时器之前到期。

I would definitely not be creating a new Thread every time the user clicked a button. If they click it a few hundred times your application might die or slow down. When they click the button you know what time you want your Timer to expire. So rather create a Timer object and put that on an ordered queue (ordered from earliest to latest expiry time) and then have a single Thread monitoring that queue, popping off Timers as they expire.

Your TimerWatcher thread can then wake up every few milliseconds, or better, wait until it knows the first Timer is going to expire and then peak at the next Timer and wait until this next one expires. Whenever you add a new Timer you can also wake up your TimerWatcher and check that the new Timer has not expired to that it doesn't expire before the one you were already waiting for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文