看一下以下兩個例子的執行結果://testthread.cs
using system;
using system.threading;
public
class test
thread.join();
console.writeline ("final count: ", count);
}
static
void threadjob()
}
}
//innerdatathread.cs
using system;using system.threading;
public
class test
", tmp);
thread.sleep(50);
tmp++;
console.writeline ("incremented tmp to ", tmp);
thread.sleep(20);
count = tmp;
console.writeline ("written count=", tmp);
thread.sleep(30);
}
thread.join();
console.writeline ("final count: ", count);
}
static
void threadjob()
", tmp);
thread.sleep(20);
tmp++;
console.writeline ("/t/t/t/tincremented tmp to ", tmp);
thread.sleep(10);
count = tmp;
console.writeline ("/t/t/t/twritten count=", tmp);
thread.sleep(40);
}
}
}
read count=0read count=0
incremented tmp to 1
written count=1
incremented tmp to 1
written count=1
read count=1
incremented tmp to 2
read count=1
written count=2
read count=2
incremented tmp to 2
incremented tmp to 3
written count=2
written count=3
read count=3
read count=3
incremented tmp to 4
incremented tmp to 4
written count=4
written count=4
read count=4
read count=4
incremented tmp to 5
written count=5
incremented tmp to 5
written count=5
read count=5
incremented tmp to 6
written count=6
final count: 6
再比較下面這個例子:
//使用monitor.enter/exit
//monitorthread.cs
using system;
using system.threading;
public
class test
", tmp);
thread.sleep(50);
tmp++;
console.writeline ("incremented tmp to ", tmp);
thread.sleep(20);
count = tmp;
console.writeline ("written count=", tmp);
monitor.exit(countlock);
thread.sleep(30);
}thread.join();
console.writeline ("final count: ", count);
}static
void threadjob()
", tmp);
thread.sleep(20);
tmp++;
console.writeline ("/t/t/t/tincremented tmp to ", tmp);
thread.sleep(10);
count = tmp;
console.writeline ("/t/t/t/twritten count=", tmp);
monitor.exit(countlock);
thread.sleep(40);}}
}結果與上例innerdatathread.cs是不一樣的,原因就在於monitor的使用了。
read count=0incremented tmp to 1
written count=1
read count=1
incremented tmp to 2
written count=2
read count=2
incremented tmp to 3
written count=3
read count=3
incremented tmp to 4
written count=4
read count=4
incremented tmp to 5
written count=5
read count=5
incremented tmp to 6
written count=6
read count=6
incremented tmp to 7
written count=7
read count=7
incremented tmp to 8
written count=8
read count=8
incremented tmp to 9
written count=9
read count=9
incremented tmp to 10
written count=10
final count: 10
下面使用lock來鎖定執行緒:
// lockthread.cs
using system;
using system.threading;
public
class test
", tmp);
thread.sleep(50);
tmp++;
console.writeline ("incremented tmp to ", tmp);
thread.sleep(20);
count = tmp;
console.writeline ("written count=", tmp);
}thread.sleep(30);
}thread.join();
console.writeline ("final count: ", count);
}static
void threadjob()
", tmp);
thread.sleep(20);
tmp++;
console.writeline ("/t/t/t/tincremented tmp to ", tmp);
if (count < 100)
throw
new exception();
thread.sleep(10);
count = tmp;
console.writeline ("/t/t/t/twritten count=", tmp);
}thread.sleep(40);}}
}結果如何?與monitorthread.cs比較一下,再想想看。
多執行緒程式設計3
執行緒清理處理函式 void pthread cleanup push void rtn void void arg 設定清理函式 void pthread cleanup pop int execute execute為0,清理函式將不被呼叫 不為0則呼叫 清理函式執行的三種情況 1,呼叫pthr...
有趣的多執行緒程式設計(2) 執行緒中的引數傳遞
使用類 類的方法或類的屬性都可以向執行緒傳遞引數 public class url public void download 在另一個類中使用它們.url new url yoururl new thread new threadstart download start 注意引數是如何傳遞的。在.n...
Linux多執行緒程式設計入門 3
3 條件變數 前一節中我們講述瞭如何使用互斥鎖來實現執行緒間資料的共享和通訊,互斥鎖一個明顯的缺點是它只有兩種狀態 鎖定和非鎖定。而條件變數通過允許執行緒阻塞和等待另一個執行緒傳送訊號的方法彌補了互斥鎖的不足,它常和互斥鎖一起使用。使用時,條件變數被用來阻塞一個執行緒,當條件不滿足時,執行緒往往解開...
Python3多執行緒程式設計
多執行緒使用,可以讓一個執行緒訪問某個資源,其他執行緒給他通過queue發任務,這樣避免對共享的資源編寫繁瑣的加鎖解鎖 threading包也提供了 locks,events,condition variables,and semaphores這些工具,可以做多執行緒間的資源共享.python有一個...
Python3多執行緒程式設計
使用多執行緒還是使用多程序,怎麼樣來控制防止執行緒太多,導致執行緒失控,就是請求一個任務,生成一個程序,最終導致程序暴漲,進而無法控制。所以,對於任務數量一直在增加的程式,固定執行緒數量的執行緒池是必要的。一些說明 最佳執行緒數的獲取 對於io密集型模型 usr bin env python3 co...