RoundRobin 方法中的线程
这是一个示例代码,如果线程大小为 3 则表示可以。我可以在哪里管理内存。如果线程大小为 50,则问题所在。我需要将线程大小设置为 5。完成的线程应该由其他人重用
Thread[] TCreate = new Thread[iThreadSize];
for (int i = 0; i< TCreate.length; i++) {
TCreate[i] = new Thread(new Runnable() {
public void run() {
lst.Add(this.getResult(url));
}
});
TCreate[i].setName("URL"+i);
TCreate[i].start(); }
for (int j = 0; j < TCreate.length; j++)
while (TCreate[j].isAlive())
Thread.sleep(10);
,任何人都可以帮助 setDaemon() 方法的用途。守护进程的目的是什么 请帮助我..提前谢谢
Here is a sample code if the thread size is 3 means its ok. where i can manage the memory. if the thread size is 50 there the problem lies. i need to set threadsize as 5. finished thread should be reuse the by other
Thread[] TCreate = new Thread[iThreadSize];
for (int i = 0; i< TCreate.length; i++) {
TCreate[i] = new Thread(new Runnable() {
public void run() {
lst.Add(this.getResult(url));
}
});
TCreate[i].setName("URL"+i);
TCreate[i].start(); }
for (int j = 0; j < TCreate.length; j++)
while (TCreate[j].isAlive())
Thread.sleep(10);
Can any one help what is use of setDaemon() method. what is purpose of Daemon
Please Help me.. Advance thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
setDaemon
控制线程是否为守护线程。如果程序到达 main 方法末尾时守护线程仍在运行,则不会阻止程序退出。非守护线程(用户线程)确实可以防止程序退出。setDaemon
controls whether a thread is a daemon thread or not. If daemon threads are still running when the program reaches the end of the main method, it does not keep the program from quitting. Non-daemon threads (user threads) do keep the program from quitting.Java 已经包含了管理线程池的方法。
调用 Executors.newFixedThreadPool(5) 将为您生成一个包含 5 个工作线程的线程池。
之后,您可以分配将由池中的线程执行的 Runnables。
另请参阅:
Java already includes methods for managing Thread pools.
calling
Executors.newFixedThreadPool(5)
will generate a thread pool with 5 worker threads for you.Afterwards you can just assign Runnables that will be executed by on of the Threads in the pool.
See also: