Static synchronized Method
来源:互联网 发布:帝国cms广告代码 编辑:程序博客网 时间:2024/05/17 23:36
Java提供了synchronized关键字用于修饰方法,使用synchronized修饰的方法被称为同步方法。当然,synchronized关键字除了修饰方法之外,还可以修饰普通代码块,使用synchronized修饰的代码块被称为同步代码块。
Java语法规定,任何线程进入同步方法,同步代码块之前,必须先获取同步方法,同步代码块对应的同步监控器。
对于同步代码块而言,程序必须显式为它指定同步监视器;对于同步非静态方法而言,该方法的同步监视器是this――即调用该方法的Java对象;对于静态的同步方法而言,该方法的同步监视器不是this,而是该类本身。
public class SynchronizedStatic implements Runnable{
static boolean staticFlag = true;
public static synchronized void test0(){
for(int i=0;i<100;i++){
System.out.println("test0:"+Thread.currentThread().getName()+""+i);
}
}
public void test1(){
synchronized(this){
for(int i=0;i<100;i++){
System.out.println("test1:"+Thread.currentThread().getName()+""+i);
}
}
}
@Override
public void run() {
// TODO Auto-generated method stub
if(staticFlag){
staticFlag=false;
test0();
}else{
staticFlag=true;
test1();
}
}
public static void main(String[] args)throws Exception{
SynchronizedStatic ss = new SynchronizedStatic();
new Thread(ss).start();
//Thread.sleep(10);
new Thread(ss).start();
}
}
test0:Thread-00
test1:Thread-10
test1:Thread-11
test1:Thread-12
test1:Thread-13
test1:Thread-14
test1:Thread-15
test0:Thread-01
test0:Thread-02
test1:Thread-16
test0:Thread-03
test1:Thread-17
test1:Thread-18
test0:Thread-04
test1:Thread-19
test0:Thread-05
test1:Thread-110
test0:Thread-06
test1:Thread-111
test0:Thread-07
test1:Thread-112
test0:Thread-08
test0:Thread-09
test1:Thread-113
test0:Thread-010
test1:Thread-114
test0:Thread-011
test1:Thread-115
test0:Thread-012
test1:Thread-116
test0:Thread-013
test1:Thread-117
test0:Thread-014
test1:Thread-118
test0:Thread-015
test1:Thread-119
test0:Thread-016
test1:Thread-120
test0:Thread-017
test0:Thread-018
test1:Thread-121
test1:Thread-122
test0:Thread-019
test1:Thread-123
test0:Thread-020
test1:Thread-124
test0:Thread-021
test1:Thread-125
test0:Thread-022
test1:Thread-126
test0:Thread-023
test1:Thread-127
test0:Thread-024
test1:Thread-128
test1:Thread-129
test0:Thread-025
test1:Thread-130
test0:Thread-026
test1:Thread-131
test1:Thread-132
test0:Thread-027
test0:Thread-028
test1:Thread-133
test1:Thread-134
test0:Thread-029
test1:Thread-135
test1:Thread-136
test0:Thread-030
test1:Thread-137
test0:Thread-031
test1:Thread-138
test0:Thread-032
test1:Thread-139
test0:Thread-033
test1:Thread-140
test0:Thread-034
test1:Thread-141
test1:Thread-142
test1:Thread-143
test1:Thread-144
test1:Thread-145
test1:Thread-146
test1:Thread-147
test0:Thread-035
test1:Thread-148
test1:Thread-149
test1:Thread-150
test1:Thread-151
test1:Thread-152
test1:Thread-153
test1:Thread-154
test1:Thread-155
test1:Thread-156
test1:Thread-157
test1:Thread-158
test1:Thread-159
test1:Thread-160
test1:Thread-161
test1:Thread-162
test1:Thread-163
test1:Thread-164
test1:Thread-165
test1:Thread-166
test1:Thread-167
test0:Thread-036
test1:Thread-168
test0:Thread-037
test0:Thread-038
test0:Thread-039
test0:Thread-040
test0:Thread-041
test0:Thread-042
test0:Thread-043
test0:Thread-044
test0:Thread-045
test0:Thread-046
test0:Thread-047
test0:Thread-048
test0:Thread-049
test0:Thread-050
test0:Thread-051
test0:Thread-052
test0:Thread-053
test0:Thread-054
test0:Thread-055
test0:Thread-056
test0:Thread-057
test0:Thread-058
test0:Thread-059
test0:Thread-060
test0:Thread-061
test0:Thread-062
test0:Thread-063
test0:Thread-064
test0:Thread-065
test0:Thread-066
test0:Thread-067
test0:Thread-068
test0:Thread-069
test0:Thread-070
test0:Thread-071
test0:Thread-072
test0:Thread-073
test0:Thread-074
test0:Thread-075
test0:Thread-076
test0:Thread-077
test1:Thread-169
test0:Thread-078
test0:Thread-079
test1:Thread-170
test1:Thread-171
test0:Thread-080
test1:Thread-172
test1:Thread-173
test1:Thread-174
test1:Thread-175
test1:Thread-176
test1:Thread-177
test0:Thread-081
test1:Thread-178
test0:Thread-082
test1:Thread-179
test0:Thread-083
test1:Thread-180
test0:Thread-084
test1:Thread-181
test0:Thread-085
test1:Thread-182
test0:Thread-086
test1:Thread-183
test0:Thread-087
test1:Thread-184
test0:Thread-088
test1:Thread-185
test1:Thread-186
test0:Thread-089
test0:Thread-090
test1:Thread-187
test0:Thread-091
test1:Thread-188
test0:Thread-092
test1:Thread-189
test0:Thread-093
test1:Thread-190
test0:Thread-094
test1:Thread-191
test0:Thread-095
test1:Thread-192
test0:Thread-096
test1:Thread-193
test0:Thread-097
test1:Thread-194
test0:Thread-098
test1:Thread-195
test0:Thread-099
test1:Thread-196
test1:Thread-197
test1:Thread-198
test1:Thread-199
静态同步方法和以this为同步监视器的同步代码块同时执行,当第一条线程进入同步代码块执行之后,该线程获得了对同步监视器的锁定。第二条线程尝试进入同步代码块执行,进入同步代码块之前,该线程必须获得对this引用(也就是ss变量所引用的对象)的锁定。因为第一条线程锁定的是SynchronizedStatic类,而不是ss变量所引用的对象,所以第二条线程完全可以获得对ss变量所引用的对象的锁定,因此系统可以切换到执行第二条线程。以下把同不快变为同步监视SynchronizedStatic类,
public void test1(){
synchronized(SynchronizedStatic.class){
for(int i=0;i<100;i++){
System.out.println("test1:"+Thread.currentThread().getName()+""+i);
}
}
}
可以看到两个线程不能同步执行,必须等到第一条线程结束之后才能启动第二条线程。
- Static synchronized Method
- java synchronized static method and common method
- synchronized method 和 synchronized static method 的区别
- synchronized与static synchronized
- synchronized与static synchronized
- synchronized object & synchronized method
- Synchronized和Static Synchronized区别
- Synchronized和Static Synchronized区别
- Synchronized和Static Synchronized区别
- Synchronized和Static Synchronized区别
- Python的method, class method, static method
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
- 黑马程序员_abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
- abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized
- abstract 的method 是否可同时是static,是否可同时是native,是否可同时是synchronized? 【基础】
- gcov、lcov与genhtml
- android 开发Eclipse "Invalid layout of java.lang.String at value"
- oracle一些常识
- RTL代码风格
- KVM虚拟机和QEMU
- Static synchronized Method
- Android gridview 图标浮出效果
- Poj2762——有向图缩点+拓扑排序
- 大数据:利用相关性的营销
- rmdir()
- NSSortDescriptor(数组排序)
- Quartz任务调度快速入门
- 个人碰到的简单问题处理,慢慢总结
- HDU-1232-畅通工程-最简单并查集问题