作者: tomtom12_34 時間: 2012-5-28 17:13 標題: java Thread 問題
- public class Test
- {
- public static void main(String[] args)
- {
- for (int i = 0; i < 3; i++)
- {
- TestThread tt = new TestThread();
- System.out.println("Main : " + tt.getName());
- tt.setName("ABC" + i);
- System.out.println("Main : " + tt.getName());
- tt.start();
- }
- // TestThread tt = ;
- }
- public static class TestThread extends Thread
- {
- public void run()
- {
- System.out.println("Thread : " + getName() + " is running.");
- }
- }
- }
前提係唔可以喺出面create 定啲thread出嚟代替for loop 入面嘅create thread
作者: sinsinking 時間: 2012-5-30 08:06
咁一係你要搵個variable sync返D thread 既野.你唔可以create定d thread咪create左個variable sync返你想要既野.
http://stackoverflow.com/questio ... fferent-instances-o
作者: 無忌 時間: 2012-5-30 13:41
你既動機好好. 如果你有個loop要不停開新thread, 最好唔好不停開, 而係用Thread Pool.
想用返之前D thread, 你要用一個Thread Pool. 個pool有D worker thread. 有人要用時問個pool拎個得閒冇野做既thread, 如果冇thread得閒就要等. 用thread pool可以節省好多object/thread creation既overhead.
http://docs.oracle.com/javase/tu ... currency/pools.html
http://developer.amd.com/documen ... ges/1121200683.aspx
作者: tomtom12_34 時間: 2012-5-30 14:58
thanks~
try try sin
