新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java線程同步引用基本代碼介紹
Java線程同步引用有很多的使用范圍,在不斷的使用中我們有很多的知識(shí)需要學(xué)習(xí)。我們可以在計(jì)算機(jī)上運(yùn)行各種計(jì)算機(jī)軟件程序。每一個(gè)運(yùn)行的程序可能包括多個(gè)獨(dú)立運(yùn)行的線程。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、涇源網(wǎng)站維護(hù)、網(wǎng)站推廣。
- public class SynTest ...{
- private String firstName, lastName;
- private synchronized void setName(String firstName, String lastName) ...{
- print("entering setName");
- this.firstName = firstName;
- print("Set first name have done firstName=" + this.firstName);
- try ...{
- Thread.sleep(1000);
- } catch (InterruptedException e) ...{
- }
- this.lastName = lastName;
- print("set last name have done,and leave setName() method.firstName="
- + this.firstName + " lastName=" + this.lastName);
- }
- private void print(String msg) ...{
- String thread = Thread.currentThread().getName();
- System.out.println(thread + ": " + msg);
- }
- public static void main(String[] args) ...{
- // 必需聲明為final,否則runnable里面的run()方法不能訪問。
- final SynTest test1 = new SynTest();
- final SynTest test2 = new SynTest();
- Runnable run1 = new Runnable() ...{
- public void run() ...{
- test1.setName("arzu", "guli");
- }
- };
- Thread threadOne = new Thread(run1, "threadOne");
- threadOne.start();
- try ...{
- Thread.sleep(200);
- } catch (InterruptedException e) ...{
- }
- Runnable run2 = new Runnable() ...{
- public void run() ...{
- // 如果這個(gè)線程引用的是對(duì)象test2,則setName方法不需要同步,也可以保證程序達(dá)到預(yù)期目的。
- test1.setName("kang", "midi");
- }
- };
- Thread threadTwo = new Thread(run2, "threadTwo");
- threadTwo.start();
- System.out.println("main() exit");
- }
- }
以上就是對(duì)Java線程同步引用的詳細(xì)介紹。
當(dāng)前名稱:Java線程同步引用基本代碼介紹
分享鏈接:http://www.5511xx.com/article/cddihog.html


咨詢
建站咨詢
