多线程问题 本帖最后由 bluecountry 于 2011-07-21 15:51:09 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 简化一下问题A a;for(2次){ a= new A();}关掉的时候a.release()只关了后面那个实例.不用数组,怎么用a把2个都关掉 public class Test { private test1 t1; public static void main(String[] args) { Test t = new Test(); t.logic(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } private void logic() { for (int i = 0; i < 2; i++) { t1 = new test1(); t1.setDaemon(true); t1.start(); } }}class test1 extends Thread { @Override public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(currentThread().getName()); } }}要只是为了让这些线程在主线程结束时停止,设置成守护线程就行了 package a;import java.util.concurrent.Executors;import java.util.concurrent.ExecutorService;public class Test { private test1 t1; private int threadCount = 2; private ExecutorService pool = Executors.newFixedThreadPool(threadCount); public static void main(String[] args) { Test t = new Test(); t.logic(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { t.endLogic(); } } private void logic() { for (int i = 0; i < threadCount; i++) { t1 = new test1(); pool.execute(t1); } } private void endLogic(){ pool.shutdownNow(); }}class test1 extends Thread { @Override public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { break; } System.out.println(currentThread().getName()); } }}线程池呢 private static boolean flag = false;flag 换成静态的试下 全排列代码求解释 Swing组件中,如何用一个BUTTON弹出一个新窗口? 请高手解决一个问题。关于final类型 java中初始化顺序详解 动态加载jpanel问题? 100分问一个JAVA最基本的难题,相信这里所有人都能回答。 虚拟机报错的问题,极度心寒,哪位高手能帮我看看这个错误 为什么action(Event evt,Object obj)方法要返回boolean? 『谁能说下 『Java中引用的定义(不是与C++中的区别)』』 也许不是问题的问题 请问如何在菜单栏和弹出菜单中添加同一个菜单项? 文本索引
A a;
for(2次){
a= new A();
}关掉的时候a.release()只关了后面那个实例.
不用数组,怎么用a把2个都关掉
private test1 t1; public static void main(String[] args) {
Test t = new Test();
t.logic();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} private void logic() {
for (int i = 0; i < 2; i++) {
t1 = new test1();
t1.setDaemon(true);
t1.start();
}
}
}class test1 extends Thread { @Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(currentThread().getName());
}
}
}要只是为了让这些线程在主线程结束时停止,设置成守护线程就行了
import java.util.concurrent.ExecutorService;
public class Test {
private test1 t1;
private int threadCount = 2;
private ExecutorService pool = Executors.newFixedThreadPool(threadCount);
public static void main(String[] args) {
Test t = new Test();
t.logic();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
t.endLogic();
}
} private void logic() {
for (int i = 0; i < threadCount; i++) {
t1 = new test1();
pool.execute(t1);
}
}
private void endLogic(){
pool.shutdownNow();
}
}class test1 extends Thread {
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
System.out.println(currentThread().getName());
}
}
}
线程池呢
flag 换成静态的试下