新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
如何實(shí)現(xiàn)Java多線性同步讀寫數(shù)據(jù)
Java多線性同步進(jìn)行是一個(gè)很重要的東西,我們不少的時(shí)候都需要用到這些代碼。但是有人還不能很好的進(jìn)行讀寫同步的相關(guān)代碼編寫。下面我們就看看如何才能正確的進(jìn)行Java多線性同步的編寫。

創(chuàng)新互聯(lián)公司主營(yíng)臨武網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,app軟件定制開發(fā),臨武h5重慶小程序開發(fā)搭建,臨武網(wǎng)站營(yíng)銷推廣歡迎臨武等地區(qū)企業(yè)咨詢
- public class SynTest ...{
- private String firstName, lastName;
- private synchronized String getName() ...{
- String result = firstName + " " + lastName;
- return result;
- }
- 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 static void print(String msg) ...{
- String thread = Thread.currentThread().getName();
- System.out.println(thread + ": " + msg);
- }
- public static void main(String[] args) ...{
- // 必需聲明為final,否則runnable里面的run()方法不能訪問(wèn)。
- final SynTest test1 = new SynTest();
- // 設(shè)置初始值
- test1.setName("arzu", "guli");
- Runnable run1 = new Runnable() ...{
- public void run() ...{
- test1.setName("kang", "midi");
- }
- };
- // 修改名字線程
- Thread threadOne = new Thread(run1, "threadOne");
- threadOne.start();
- try ...{
- Thread.sleep(200);
- } catch (InterruptedException e) ...{
- }
- Runnable run2 = new Runnable() ...{
- public void run() ...{
- print("讀取" + test1.getName());
- }
- };
- // 讀取名字線程
- Thread threadTwo = new Thread(run2, "threadTwo");
- threadTwo.start();
- System.out.println("main() exit");
- }
- }
以上就是對(duì)Java多線性同步的詳細(xì)介紹。
網(wǎng)站題目:如何實(shí)現(xiàn)Java多線性同步讀寫數(shù)據(jù)
文章源于:http://www.5511xx.com/article/djgcjpo.html


咨詢
建站咨詢
