java的多線程訪問共享變量_java多線程通信之共享變量

(1)當訪問共同的代碼的時候:可以使用同一個Runnable對象,這個Runnable對象中有這個共享數據,比如賣票系統就可以這么做。或者這個共享數據封裝在一個對象當中,然后對這個對象加鎖,也可以實現數據安全訪問。public?class?Interfacaesharethread?{

public?static?void?main(String[]?args)?{

Mythread1?mythread?=?new?Mythread1();

new?Thread(mythread).start();

new?Thread(mythread).start();

new?Thread(mythread).start();

new?Thread(mythread).start();

}

}

/*?實現Runnable接口?*/

class?Mythread1?implements?Runnable?{

int?index?=?0;

public?synchronized?void?run()?{

while?(true)

System.out.println(Thread.currentThread().getName()?+?"is?running?and?the?index?is?"?+?index++);

}

}

(2)當各個線程訪問的代碼不同時:這時候要使用不同的Runnable對象,

將Runnable對象作為某一個類中的內部類,共享數據作為這個外部類的成員變量,每個線程對共享數據的訪問也交給外部類的方法,比便對共享數據的互斥和通信。Runnable對象調用外部類的操作這個共享數據的方法。

public?class?Innersharethread?{

public?static?void?main(String[]?args)?{

Mythread?mythread?=?new?Mythread();

mythread.getThread().start();

mythread.getThread().start();

mythread.getThread().start();

mythread.getThread().start();

}

}

/**

*?通過內部類實現線程的共享變量

*

*/

class?Mythread?{

int?index?=?0;

private?class?InnerThread?extends?Thread?{

public?synchronized?void?run()?{

while?(true)?{

System.out.println(Thread.currentThread().getName()

+?"is?running?and?index?is?"?+?index++);

}

}

}

public?Thread?getThread()?{

return?new?InnerThread();

}

}

參考文章:

原文:http://tianxingzhe.blog.51cto.com/3390077/1693465

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388773.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388773.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388773.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

2018年阿里云NoSQL數據庫大事盤點

2019獨角獸企業重金招聘Python工程師標準>>> NoSQL一詞最早出現在1998年。2009年Last.fm的Johan Oskarsson發起了一次關于分布式開源數據庫的討論,來自Rackspace的Eric Evans再次提出了NoSQL概念,這時的NoSQL主要是指非關系型、分布式、不提供…

cayenne:用于隨機模擬的Python包

TL;DR; We just released v1.0 of cayenne, our Python package for stochastic simulations! Read on to find out if you should model your system as a stochastic process, and why you should try out cayenne.TL; DR; 我們剛剛發布了 cayenne v1.0 ,這是我們…

java 如何將word 轉換為ftl_使用 freemarker導出word文檔

近日需要將人員的基本信息導出,存儲為word文檔,查閱了很多資料,最后選擇了使用freemarker,網上一共有四種方式,效果都一樣,選擇它呢是因為使用簡單,再次記錄一下,一個簡單的demo,僅供…

DotNetBar office2007效果

1.DataGridView 格式化顯示cell里的數據日期等。 進入編輯列,選擇要設置的列,DefaultCellStyle里->行為->formart設置 2.tabstrip和mdi窗口的結合使用給MDI窗口加上TabPage。拖動個tabstrip到MDI窗口上tabstrip里選擇到主窗口名就加上TABPAGE了。d…

Spring boot 中pom.xml 各個節點詳解

<project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd "> <!-- 父項目的坐…

spotify 數據分析_沒有數據? 沒問題! 如何從Wikipedia和Spotify收集重金屬數據

spotify 數據分析For many data science students, collecting data is seen as a solved problem. It’s just there in Kaggle or UCI. However, that’s not how data is available daily for working Data Scientists. Also, many of the datasets used for learning have …

stack 的一些用法

#include<bits/stdc.h> using namespace std; int32_t main() {stack<int> st;st.push(1);st.push(2);st.push(3);cout<<st.size()<<endl;while(!st.empty()){cout<<st.top()<<endl;st.pop();} } 轉載于:https://www.cnblogs.com/Andromed…

IS環境下配置PHP5+MySql+PHPMyAdmin

IIS環境下配置PHP5MySqlPHPMyAdmin Posted on 2009-08-07 15:18 謝啟祥 閱讀(1385)評論(18) 編輯 收藏 雖然主要是做.net開發的&#xff0c;但是&#xff0c;時不時的還要搞一下php&#xff0c;但是&#xff0c;php在windows下的配置&#xff0c;總是走很多彎路&#xff0c;正好…

js復制功能

<div id"cardList"><div class"btn" onClick"copy(111)">點擊我&#xff0c;復制我</div></div> <script type"text/javascript"> function copy(str){var save function (e){e.clipboardData.setDa…

input在iOS里的兼容性

input框在iOS里&#xff0c;無法聚焦&#xff0c;不能輸入內容&#xff0c;把-webkit-user-select:none改成-webkit-user-select:auto;或者直接加一個style“-webkit-user-select:auto”.

kaggle數據集_Kaggle上有170萬份ArXiv文章的數據集

kaggle數據集“arXiv is a free distribution service and an open-access archive for 1.7 million scholarly articles in the fields of physics, mathematics, computer science, quantitative biology, quantitative finance, statistics, electrical engineering and sys…

java用接口實例化對象_[求助]迷茫中,接口可以直接實例化對象嗎?

可能是我沒有寫完整吧,還是我沒有理解好1 接口public interface SetAndGetWeight{public void setW(double weight);public double getW();}2 類class Train{SetAndGetWeight[] things;public void Train(SetAndGetWeight[] things){this.thingsthings;}public void returnTota…

異常作業2(2018.08.22)

2、編寫程序接收用戶輸入分數信息&#xff0c;如果分數在0—100之間&#xff0c; 輸出成績。如果成績不在該范圍內&#xff0c; 拋出異常信息&#xff0c;提示分數必須在0—100之間。 要求&#xff1a;使用自定義異常實現1 import java.util.Scanner;2 3 class AtException ext…

深度學習數據集中數據差異大_使用差異隱私來利用大數據并保留隱私

深度學習數據集中數據差異大The modern world runs on “big data,” the massive data sets used by governments, firms, and academic researchers to conduct analyses, unearth patterns, and drive decision-making. When it comes to data analysis, bigger can be bett…

C#圖片處理基本應用(裁剪,縮放,清晰度,水印)

前言 需求源自項目中的一些應用&#xff0c;比如相冊功能&#xff0c;通常用戶上傳相片后我們都會針對該相片再生成一張縮略圖&#xff0c;用于其它頁面上的列表顯示。隨便看一下&#xff0c;大部分網站基本都是將原圖等比縮放來生成縮略圖。但完美主義者會發現一些問題&#…

java建立tcp服務器長連接_B/S 架構下后端能否建立 TCP 長連接?

這種架構下&#xff0c;這樣的優化策略能實現嗎&#xff1f;能有作用嗎&#xff1f;php 服務端請求 ES tcp server 部分代碼$streamClient stream_socket_client("tcp://{$tcpHost}:{$tcpPort}", $errno, $errstr);// 該數組是所有業務線的分類結構&#xff0c;及每…

Java客戶端訪問HBase集群解決方案(優化)

測試環境&#xff1a;IdeaWindows10 準備工作&#xff1a; <1>、打開本地 C:\Windows\System32\drivers\etc&#xff08;系統默認&#xff09;下名為hosts的系統文件&#xff0c;如果提示當前用戶沒有權限打開文件&#xff1b;第一種方法是將hosts文件拖到桌面進行配置后…

WPF布局系統

WPF之路——WPF布局系統 前言 前段時間忙了一陣子Google Earth&#xff0c;這周又忙了一陣子架構師論文開題報告&#xff0c;現在終于有時間繼續<WPF之路>了。先回憶一下上篇的內容&#xff0c;在《從HelloWorld到WPF World》中&#xff0c;我們對WPF有了個大概的了解&am…

PostGIS容器運行

2019獨角獸企業重金招聘Python工程師標準>>> 獲取鏡像&#xff1a; docker pull mdillon/postgis 該 mdillon/postgis 鏡像提供了容器中運行Postgres&#xff08;內置安裝PostGIS 2.5&#xff09; 。該鏡像基于官方 postgres image&#xff0c;提供了多種變體&#…

小型數據庫_如果您從事“小型科學”工作,那么您是否正在利用數據存儲庫?

小型數據庫If you’re a scientist, especially one performing a lot of your research alone, you probably have more than one spreadsheet of important data that you just haven’t gotten around to writing up yet. Maybe you never will. Sitting idle on a hard dri…