- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章java多线程编程之Synchronized块同步方法由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
文章分享了4个例子对synchronized的详细解释 。
1、是否加synchronized关键字的不同 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
public
class
ThreadTest {
public
static
void
main(String[] args) {
Example example =
new
Example();
Thread t1 =
new
Thread1(example);
Thread t2 =
new
Thread1(example);
t1.start();
t2.start();
}
}
class
Example {
public
synchronized
void
excute() {
for
(
int
i =
0
; i <
5
; ++i) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"excute:"
+ i);
}
}
}
class
Thread1
extends
Thread {
private
Example example;
public
Thread1(Example example) {
this
.example = example;
}
@Override
public
void
run() {
example.excute();
}
}
|
加了synchronized关键字的输出结果如下 。
会先输出一组0-4,接着再输出下一组,两个线程顺序执行 。
excute:0 excute:1 excute:2 excute:3 excute:4 excute:0 excute:1 excute:2 excute:3 excute:4 。
没加synchronized关键字的输出结果如下 。
两个线程同时执行excute方法,同时并发的 。
excute:0 excute:0 excute:1 excute:1 excute:2 excute:2 excute:3 excute:3 excute:4 excute:4 。
2、多个方法的多线程情况 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
public
class
ThreadTest {
public
static
void
main(String[] args) {
Example example =
new
Example();
Thread t1 =
new
Thread1(example);
Thread t2 =
new
Thread2(example);
t1.start();
t2.start();
}
}
class
Example {
public
synchronized
void
excute() {
for
(
int
i =
0
; i <
5
; ++i) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"excute:"
+ i);
}
}
public
synchronized
void
excute1() {
for
(
int
i =
0
; i <
5
; ++i) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"excute1:"
+ i);
}
}
}
class
Thread1
extends
Thread {
private
Example example;
public
Thread1(Example example) {
this
.example = example;
}
@Override
public
void
run() {
example.excute();
}
}
class
Thread2
extends
Thread {
private
Example example;
public
Thread2(Example example) {
this
.example = example;
}
@Override
public
void
run() {
example.excute1();
}
}
|
执行结果如下 。
同样是顺序执行,执行完一个线程再执行另一个线程 。
excute:0 excute:1 excute:2 excute:3 excute:4 excute1:0 excute1:1 excute1:2 excute1:3 excute1:4 。
如果去掉synchronized关键字,则两个方法并发执行,并没有相互影响.
但是如例子程序中所写,即便是两个方法:
执行结果永远是执行完一个线程的输出再执行另一个线程的。 。
说明:
如果一个对象有多个synchronized方法,某一时刻某个线程已经进入到了某个synchronized方法,那么在该方法没有执行完毕前,其他线程是无法访问该对象的任何synchronized方法的.
结论:
当synchronized关键字修饰一个方法的时候,该方法叫做同步方法.
Java中的每个对象都有一个锁(lock),或者叫做监视器(monitor),当一个线程访问某个对象的synchronized方法时,将该对象上锁,其他任何线程都无法再去访问该对象的synchronized方法了(这里是指所有的同步方法,而不仅仅是同一个方法),直到之前的那个线程执行方法完毕后(或者是抛出了异常),才将该对象的锁释放掉,其他线程才有可能再去访问该对象的synchronized方法.
注意这时候是给对象上锁,如果是不同的对象,则各个对象之间没有限制关系.
尝试在代码中构造第二个线程对象时传入一个新的Example对象,则两个线程的执行之间没有什么制约关系.
3、静态的同步方法 。
当一个synchronized关键字修饰的方法同时又被static修饰,之前说过,非静态的同步方法会将对象上锁,但是静态方法不属于对象,而是属于类,它会将这个方法所在的类的Class对象上锁.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
public
class
ThreadTest {
public
static
void
main(String[] args) {
Example example =
new
Example();
Example example2 =
new
Example();
Thread t1 =
new
Thread1(example);
Thread t2 =
new
Thread2(example2);
t1.start();
t2.start();
}
}
class
Example {
public
synchronized
static
void
excute() {
for
(
int
i =
0
; i <
5
; ++i) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"excute:"
+ i);
}
}
public
synchronized
static
void
excute1() {
for
(
int
i =
0
; i <
5
; ++i) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"excute1:"
+ i);
}
}
}
class
Thread1
extends
Thread {
private
Example example;
public
Thread1(Example example) {
this
.example = example;
}
@Override
public
void
run() {
example.excute();
}
}
class
Thread2
extends
Thread {
private
Example example;
public
Thread2(Example example) {
this
.example = example;
}
@Override
public
void
run() {
example.excute1();
}
}
|
执行结果如下 。
excute:0 excute:1 excute:2 excute:3 excute:4 excute1:0 excute1:1 excute1:2 excute1:3 excute1:4 。
如果没有static修饰符,两个线程分别传入不同的对象,则会同时并发执行 。
所以如果是静态方法的情况(execute()和execute2()都加上static关键字),即便是向两个线程传入不同的Example对象,这两个线程仍然是互相制约的,必须先执行完一个,再执行下一个.
结论:
如果某个synchronized方法是static的,那么当线程访问该方法时,它锁的并不是synchronized方法所在的对象,而是synchronized方法所在的类所对应的Class对象。Java中,无论一个类有多少个对象,这些对象会对应唯一一个Class对象,因此当线程分别访问同一个类的两个对象的两个static,synchronized方法时,它们的执行顺序也是顺序的,也就是说一个线程先去执行方法,执行完毕后另一个线程才开始.
4.synchronized块 。
synchronized(object) 。
{ 。
} 。
表示线程在执行的时候会将object对象上锁。(注意这个对象可以是任意类的对象,也可以使用this关键字).
这样就可以自行规定上锁对象.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
public
class
ThreadTest {
public
static
void
main(String[] args) {
Example example =
new
Example();
Thread t1 =
new
Thread1(example);
Thread t2 =
new
Thread2(example);
t1.start();
t2.start();
}
}
class
Example {
public
void
excute() {
synchronized
(
this
) {
for
(
int
i =
0
; i <
5
; ++i) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"excute:"
+ i);
}
}
}
public
void
excute1() {
synchronized
(
this
) {
for
(
int
i =
0
; i <
5
; ++i) {
try
{
Thread.sleep(
1000
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
System.out.println(
"excute1:"
+ i);
}
}
}
}
class
Thread1
extends
Thread {
private
Example example;
public
Thread1(Example example) {
this
.example = example;
}
@Override
public
void
run() {
example.excute();
}
}
class
Thread2
extends
Thread {
private
Example example;
public
Thread2(Example example) {
this
.example = example;
}
@Override
public
void
run() {
example.excute1();
}
}
|
执行结果如下 。
excute:0 excute:1 excute:2 excute:3 excute:4 excute1:0 excute1:1 excute1:2 excute1:3 excute1:4 。
例子程序4所达到的效果和例子程序2的效果一样,都是使得两个线程的执行顺序进行,而不是并发进行,当一个线程执行时,将object对象锁住,另一个线程就不能执行对应的块.
synchronized方法实际上等同于用一个synchronized块包住方法中的所有语句,然后在synchronized块的括号中传入this关键字。当然,如果是静态方法,需要锁定的则是class对象。 。
可能一个方法中只有几行代码会涉及到线程同步问题,所以synchronized块比synchronized方法更加细粒度地控制了多个线程的访问,只有synchronized块中的内容不能同时被多个线程所访问,方法中的其他语句仍然可以同时被多个线程所访问(包括synchronized块之前的和之后的).
结论:
synchronized方法是一种粗粒度的并发控制,某一时刻,只能有一个线程执行该synchronized方法; 。
synchronized块则是一种细粒度的并发控制,只会将块中的代码同步,位于方法内、synchronized块之外的其他代码是可以被多个线程同时访问到的.
以上就是关于java多线程编程Synchronized块同步方法,希望对大家的学习有所帮助.
最后此篇关于java多线程编程之Synchronized块同步方法的文章就讲到这里了,如果你想了解更多关于java多线程编程之Synchronized块同步方法的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!