Java中Synchronized的用法

2021-08-20 02:16:02 字數 4106 閱讀 1374

/**

* 同步執行緒

*/class syncthread implements runnable

public

void

run() catch (interruptedexception e) }}

}public

intgetcount()

}

syncthread syncthread =

new syncthread();

thread thread1 =

newthread(syncthread, "syncthread1");

thread thread2 =

newthread(syncthread, "syncthread2");

thread1.start();

thread2.start();

thread thread1 =

newthread(new syncthread(), "syncthread1");

thread thread2 =

newthread(new syncthread(), "syncthread2");

thread1.start();

thread2.start();

syncthread syncthread1 = new syncthread();

syncthread syncthread2 = new syncthread();

thread thread1 = new thread(syncthread1, "syncthread1");

thread thread2 = new thread(syncthread2, "syncthread2");

thread1.start();

thread2.start();

class

counter

implements

runnable

public

void countadd() catch (interruptedexception e) }}

}//非synchronized**塊,未對count進行讀寫操作,所以可以不用synchronized

public

void printcount() catch (interruptedexception e) }}

public

void run() else

if (threadname.equals("b"))

}}

counter counter =

new counter();

thread thread1 =

newthread(counter, "a");

thread thread2 =

newthread(counter, "b");

thread1.start();

thread2.start();

/**

* 銀行賬戶類

*/class account

//存錢

public

void

deposit(float amt) catch (interruptedexception e)

}//取錢

public

void

withdraw(float amt) catch (interruptedexception e)

}public

float

getbalance()

}/**

* 賬戶操作類

*/class accountoperator implements runnable

public

void

run()

}}

account account = new account("zhang san", 10000.0f);

accountoperator accountoperator = new accountoperator(account);

final

int thread_num = 5;

thread threads = new thread[thread_num];

for (int i = 0; i < thread_num; i ++)

public

void

method3(someobject obj)

}

class

test

implements

runnable

}public

void run()

}

public synchronized void

run() catch (interruptedexception e)

}}

public synchronized void method

()

public void method

()}

class

parent

}class

child

extends

parent

}

class

parent

}class

child

extends

parent

}

構造方法不能使用synchronized關鍵字,但可以使用synchronized**塊來進行同步。 

public synchronized static void method

()

/**

* 同步執行緒

*/class syncthread implements runnable

public

synchronized

static

void

method() catch (interruptedexception e) }}

public

synchronized

void

run()

}

syncthread syncthread1 =

new syncthread();

syncthread syncthread2 =

new syncthread();

thread thread1 =

newthread(syncthread1, "syncthread1");

thread thread2 =

newthread(syncthread2, "syncthread2");

thread1.start();

thread2.start();

class classname 

}}

/**

* 同步執行緒

*/class syncthread implements runnable

public

static

void

method() catch (interruptedexception e) }}

}public

synchronized

void

run()

}

****************************************talk is cheap, show me the code***********************************====

Java執行緒同步問題synchronized

android usb 讀寫以前都是一讀一寫,但有些機器會出問題。就採用讀寫非同步的方法。使用物件鎖,object自帶的,然後使用object的方法wait和notify notifyall 使用方法簡單,記錄下 public synchronized int lra setregister int...

Java學習之執行緒鎖 synchronized

同步 併發 多個執行緒訪問同乙份資源 確保資源安全 執行緒安全 synchronized 同步 1 同步塊 synchronized 引用型別 this 類.class 2 同步方法 public synchronized void test public class testsyn class t...

Java中equalsIgnoreCase的用法

equalsignorecase public boolean equalsignorecase string anotherstring 將此 string 與另乙個 string 進行比較,不考慮大小寫。如果兩個字串的長度相等,並且兩個字串中的相應字元都相等 忽略大小寫 則認為這兩個字串是相等的...