可以单独访问的多个线程
我有一个按钮,按下该按钮后,应该创建一个新线程的实例:倒计时器。问题是,我需要使用某种动态数组来执行此操作,因为无法知道用户将按下按钮多少次!
这是按钮的操作侦听器中的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
ArrayList
。类似这样的事情应该做:
创建一个
List
来存储计数器:添加新的计数器线程,如下所示:
Try using an
ArrayList
.Something like this should do:
Create a
List
to store the counters:Add new counter-threads like this:
创建一个存储列表,
将线程添加到列表中,单击“
同时尝试管理从列表中删除线程的引用”
Create a storage list
add thread to list, on click
Also try to manage deleting reference of thread from
List
我绝对不会在每次用户单击按钮时创建一个新线程。如果他们点击它几百次,您的应用程序可能会死掉或变慢。当他们单击按钮时,您知道您希望计时器何时到期。因此,创建一个 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.