多线程问题 本帖最后由 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 换成静态的试下 使用lock的 线程同步问题 数据转换 原来是这样,值得学习 package&classpath问题!!! 初级问题 java编程思想中的一个正则表达式 有点变态,动态创建combobox 请问一个线程的问题。 请问要在记事本里使用包文件*.jar,应如何设置? 如何保证在文本框里面输入的是数字,而不接受其他的内容? 请问如何在菜单栏和弹出菜单中添加同一个菜单项? 文本索引
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 换成静态的试下