精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-15
有没有线程间通信的机制?
现成的类有吗? 比方说ThreadGroup可以利用吗 |
|
返回顶楼 | |
发表时间:2009-12-15
@Test public void helloThreads() throws InterruptedException { final int threads = 9; final int times = 6; ExecutorService service = Executors.newFixedThreadPool(threads); final Lock lock = new ReentrantLock(); final Condition[] conditions = new Condition[threads]; for (int i = 0; i < conditions.length; i++) { conditions[i] = lock.newCondition(); } Runnable[] runs = new Runnable[threads]; for (int i = 0; i < runs.length; i++) { runs[i] = new Runer(i) { @Override public void run() { int t = times; while (t-- > 0) { lock.lock(); try { conditions[index].await(); //work System.out.print(Thread.currentThread().getName()); System.out.print(" "); int nextIndex = index+1; if(nextIndex==threads){ nextIndex = 0; //for beautiful System.out.println(); } //give a signal to next thread conditions[nextIndex].signalAll(); } catch(Exception ex){ ex.printStackTrace(); } finally { lock.unlock(); } } } }; } for(Runnable r:runs){ service.execute(r); } //first one lock.lock(); conditions[0].signalAll(); lock.unlock(); //hold on Thread.sleep(Long.MAX_VALUE); } abstract class Runer implements Runnable{ protected final int index; public Runer(int index){ this.index = index; } } |
|
返回顶楼 | |
发表时间:2009-12-16
其实我有个恶心的解法:
公用方法D{ System.out.println("abc"); } 线程A{ 公用方法D } 线程B{ 公用方法D } 线程C{ 公用方法D } |
|
返回顶楼 | |
发表时间:2010-03-01
swen00 写道 闲来也写个,我这不用同步实现..
public class TestThread { static int num = 1; public static void main(String ds[]){ new DemoThread(1,"A").start(); new DemoThread(2,"B").start(); new DemoThread(0,"C").start(); } static class DemoThread extends Thread { int id; String pintln_str; public DemoThread(int id,String pintln_str) { this.id = id; this.pintln_str = pintln_str; } int print_num = 10; public void run() { while (print_num > 0) { if (num%3 == id) { System.out.print(pintln_str); num ++ ; print_num--; } } } } } 浏览了所有回帖,发现这个是最精炼的。 |
|
返回顶楼 | |
发表时间:2010-03-03
最后修改:2010-03-04
public class ThreadPrintABC { public static void main(String args[]) { Print p = new Print(); new PrintThread(p); new PrintThread(p); new PrintThread(p); } } /** * 打印类,打印类负责打印的内容。 * @author * */ class Print { String word = "A"; void print() { System.out.print(word); if (word.equals("A")) { word = "B"; } else if (word.equals("B")) { word = "C"; } else if (word.equals("C")) { word = "A"; } } } /** * 打印线程,只负责调用打印类。不负责打印内容。 * @author * */ class PrintThread implements Runnable { Print p; Thread t; PrintThread(Print p) { this.p = p; t = new Thread(this); t.start(); } public void run() { int j = 10; while (j > 0) { synchronized (p) {//同步 重要 不然打印顺序会乱 p.print(); j--; } } } } |
|
返回顶楼 | |
发表时间:2010-04-24
不输出ABC啦,就输出A0A1A2....
package xunlei; import java.util.ArrayList; class CtrlStruct { private Object objLock = new Object(); private int threadCount; private int loopCount; private int hasRunTimes; public int getThreadCount() { return threadCount; } public void setThreadCount(int threadCount) { this.threadCount = threadCount; } public int getLoopCount() { return loopCount; } public void setLoopCount(int loopCount) { this.loopCount = loopCount; } public Object getObjLock() { return objLock; } public int getHasRunTimes() { return hasRunTimes; } public void setHasRunTimes(int threadCount) { this.hasRunTimes = threadCount; } } class OutputName extends Thread { private int index; private CtrlStruct tt; public OutputName(int index, CtrlStruct tt) { super("A" + index); this.index = index; this.tt = tt; } public void run() { int count = 0; synchronized (tt.getObjLock()) { while (count < tt.getLoopCount()) { // 不该打印自己,则等待 while (index != tt.getHasRunTimes() % tt.getThreadCount()) { try { tt.getObjLock().wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count++; tt.setHasRunTimes((tt.getHasRunTimes()) + 1); System.out.print(Thread.currentThread().getName()); tt.getObjLock().notifyAll(); } } } } public class Test { public static void main(String[] args) { int threadCount = 3; int loopCount = 10; ArrayList list = new ArrayList(); CtrlStruct cs = new CtrlStruct(); for (int i = 0; i < threadCount; i++) { OutputName opn = new OutputName(i, cs); list.add(opn); } cs.setLoopCount(loopCount); cs.setThreadCount(threadCount); cs.setHasRunTimes(0); for (int i = 0; i < list.size(); i++) { ((Thread) list.get(i)).start(); } } } |
|
返回顶楼 | |
发表时间:2010-06-03
public class TestThread {
static Boolean[] str = new Boolean[]{true,false,false}; public static void main(String[] args) throws InterruptedException { Thread1 t1 = new Thread1(); Thread2 t2 = new Thread2(); Thread3 t3 = new Thread3(); t1.start(); // //t1.join(); t2.start(); // //t2.join(); t3.start(); //t3.join(); } static class Thread1 extends Thread{ public void run(){ int i=10; while(i>0){ synchronized (str) { if(str[0]){ System.out.print("A"); i--; str[0] =false ; str[1] = true; str.notifyAll(); }else{ try { str.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } static class Thread2 extends Thread{ public void run(){ int i=10; while(i>0){ synchronized (str) { if(str[1]){ System.out.print("B"); i--; str[1] = false ; str[2] = true; str.notifyAll(); }else{ try { str.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } static class Thread3 extends Thread{ public void run(){ int i=10; while(i>0){ synchronized (str) { if(str[2]){ System.out.print("C"); i--; str[2] = false; str[0] = true; str.notifyAll(); }else{ try { str.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } try { sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |
|
返回顶楼 | |