ThreadTest
public class ThreadTest{
public static void main(String args[]){
System.out.println("Hello Word");
String tname = Thread.currentThread().getName();
System.out.println(tname);
int tnumber = Thread.activeCount();
System.out.println(tnumber);
}
}
Thread.currentThread().getName();
取得目前執行序名稱
Thread.activeCount();
取得目前執行序有多少可用
------------------------------------------------------------------------------------------------------------------
取自於 猛虎出閘 6
ThreadTest4
public class ThreadTest4{
public static void main(String args[]){
HelloTheard r1 = new HelloTheard();
HelloTheard r2 = new HelloTheard();
Thread t1 = new Thread(r1, "T1");
Thread t2 = new Thread(r2, "T2");
t1.start();
t2.start();
}
}
class HelloTheard extends Thread{
public void run(){
for(int i = 1 ; i <= 10 ; i++){
String tname = Thread.currentThread().getName();
System.out.println(i+" "+tname);
}
}
}
Thread t1 = new Thread(r1, "T1");
Thread t2 = new Thread(r2, "T2");
建立Thread物件
class HelloTheard implements Runnable{
public void run(){
for(int i = 1 ; i <= 10 ; i++){
String tname = Thread.currentThread().getName();
System.out.println(i+" "+tname);
}
}
}
Thread Runnable
-----------------------------------------------------------------------------------------------------------------------
class FatherThead implements Runnable{
public void run(){
System.out.println("Go Home");
System.out.println("standardBath");
System.out.println("No Fire");
System.out.println("Cell phone");
Thread worker = new Thread(new WorkerThread());
System.out.println("Waite");
worker.start();
[ ]
System.out.println("beingBath");
System.out.println("OverBath");
}
}
class WorkerThread implements Runnable{
public void run(){
System.out.println("fire trantfor");
try{
for(int i = 1 ; i <=5 ; i++){
Thread.sleep(1000);
System.out.println(i + "min");
}
}catch(InterruptedException ie){
System.out.println("AcError");
}
System.out.println("\nfire is come home\nfire is over\n");
}
}
public class Shower{
public static void main(String args[]){
Thread father = new Thread(new FatherThead());
father.start();
}
}
---------------------
Go Home
standard Bath
No Fire
Cell phone
Waite
beingBath
OverBath
fire trantfor
1min
2min
3min
4min
5min
fire is come home
fire is over
-------------
[ ] 加上Thread.yied(); 後
Go Home
standardBath
No Fire
Cell phone
Waite
beingBath
fire trantfor
OverBath
1min
2min
3min
4min
5min
fire is come home
fire is over
---------------------
17: error: '(' expected
public void run{
^
class WorkerThread implements Runnable{
public void run(){
....
}
}
run是方法, 記得加上()
11: error: incompatible types: String cannot be converted to long
Thread.sleep("1000");
13: error: package system does not exist
system.out.println("No bath");
^
---------------------------------------------------
class WithDraw implements Runnable{
private Account account;
private double withdrawMoney;
public WithDraw(Account account, double withdrawMoney){
this.account = account;
this.withdrawMoney = withdrawMoney;
}
public void run(){
account.withDraw(account,withdrawMoney);
}
}
class Account{
private double balance;
public Account(double balance){
this.balance = balance;
}
public void withDraw(Account account, double withdrawMoney){
String tName = Thread.currentThread().getName();
System.out.println(tName+" Start ");
double tmpBalance = balance;
for(double d = 0 ; d < 99999999 ; d++);
tmpBalance = tmpBalance - withdrawMoney;
if(tmpBalance < 0){
System.out.println("------------\n"+" No Money "+"\n------------");
}else{
balance = tmpBalance;
}
System.out.println(tName+" OutMoney: "+withdrawMoney+"$, Overage "+ account.checkAccount());
}
public double checkAccount(){
return balance;
}
}
public class MagicMachine{
public static void main(String args[]){
Account ac = new Account(10000);
System.out.println("Original money: "+ ac.checkAccount()+"$" );
WithDraw s1 = new WithDraw(ac,5000);
WithDraw s2 = new WithDraw(ac,2000);
WithDraw s3 = new WithDraw(ac,4000);
Thread t1 = new Thread(s1,"T1");
Thread t2 = new Thread(s2,"T2");
Thread t3 = new Thread(s3,"T3");
t1.start();
t2.start();
t3.start();
}
}
Original money: 10000.0$
T1 Start
T3 Start
T2 Start
T1 OutMoney: 5000.0$, Overage 5000.0
T2 OutMoney: 2000.0$, Overage 8000.0
T3 OutMoney: 4000.0$, Overage 6000.0
如果說for迴圈數目太小, 就會發生
Original money: 10000.0$
T1 Start
T2 Start
OutMoney: 5000.0$, Overage 5000.0
T1 Over
------------
OutMoney: 2000.0$, Overage 3000.0
T2 Over
------------
T3 Start
------------
No Money
------------
OutMoney: 4000.0$, Overage 3000.0
T3 Over
------------
速度太快使得有些執行序會跑兩次
沒有留言:
張貼留言