发布于2021-05-29 23:46 阅读(892) 评论(0) 点赞(18) 收藏(5)
①、其他线程对“该对象”的该“synchronized方法”或者“synchronized代码块”的访问将被阻塞。
package com.wwj.text;
public class SynchronizedDemo {
public static void main(String[] args){
ThreadDemo runnable = new ThreadDemo();
Thread t1 = new Thread(runnable , "t1"); //t1基于runnable这个Runnable对象
Thread t2 = new Thread(runnable , "t2"); //t2是基于runnable这个Runnable对象
t1.start();
t2.start();
}
}
class ThreadDemo implements Runnable{
@Override
public void run() {
synchronized (this){
try{
for(int i=0 ; i<5 ; i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + "loop" + i);
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
t1loop0
t1loop1
t1loop2
t1loop3
t1loop4
t2loop0
t2loop1
t2loop2
t2loop3
t2loop4
Process finished with exit code 0
package com.wwj.text;
public class SynchronizedDemo {
public static void main(String[] args){
// ThreadDemo runnable = new ThreadDemo();
Thread t1 = new Thread( new ThreadDemo() , "t1");
Thread t2 = new Thread( new ThreadDemo(),"t2"); //t1、t2是基于不同的ThreadDeamo对象,
t1.start();
t2.start();
}
}
class ThreadDemo implements Runnable{
@Override
public void run() {
synchronized (this){
try{
for(int i=0 ; i<5 ; i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName() + " loop" +":"+ i);
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
t2 loop:0
t1 loop:0
t2 loop:1
t1 loop:1
t2 loop:2
t1 loop:2
t2 loop:3
t1 loop:3
t2 loop:4
t1 loop:4
pulbic class Something { //假设,Something有两个实例x和y。分析下面4组表达式获取的锁的情况。
public synchronized void isSyncA(){} //实例方法
public synchronized void isSyncB(){} //实例方法
public static synchronized void cSyncA(){} //静态方法
public static synchronized void cSyncB(){} //静态方法
}
package com.wwj.text;
public class SynchronizedTest {
SomeThing x = new SomeThing();
SomeThing y = new SomeThing();
private void test1(){
Thread t11 = new Thread(new Runnable() {
@Override
public void run() {
x.isSyncA();
}
} , "t11");
Thread t12 = new Thread(new Runnable() {
@Override
public void run() {
x.isSyncB();
}
},"t12");
t11.start();
t12.start();
}
private void test2(){
Thread t21 = new Thread(new Runnable() {
@Override
public void run() {
x.isSyncA();
}
},"t21");
Thread t22 = new Thread(new Runnable() {
@Override
public void run() {
y.isSyncA();
}
},"t22");
t21.start();
t22.start();
}
private void test3(){
Thread t31 = new Thread(new Runnable() {
@Override
public void run() {
SomeThing.cSyncA();
}
},"t31");
Thread t32 = new Thread(new Runnable() {
@Override
public void run() {
SomeThing.cSyncB();
}
},"t32");
t31.start();
t32.start();
}
private void test4(){
Thread t41 = new Thread(new Runnable() {
@Override
public void run() {
x.isSyncA();
}
},"t41");
Thread t42 = new Thread(new Runnable() {
@Override
public void run() {
SomeThing.cSyncA();
}
},"t42");
t41.start();
t42.start();
}
/*
test1:不能同时访问,因为isSyncA()和isSyncB()都是对同一个对象(对象x)的同步锁
test2:可以同时访问,因为访问的不是同一个对象的同步锁,x.isSyncA()是对x的同步锁,而y.isSyncA()是对y的同步锁。
test3:不可以同时访问,因为cSyncB()和cSyncA()都是静态方法,也就是对类(Something)进行加锁
test4:可以同时访问,因为isSyncA()是实例方法,x.isSyncA()使用的是对象x的锁;而cSyncA()是静态方法,Something.cSyncA()可以理解对使用的是“类的锁”。因此,它们是可以被同时访问的
*/
public static void main(String[] args){
SynchronizedTest synchronizedTest = new SynchronizedTest();
//synchronizedTest.test1();
synchronizedTest.test2();
//synchronizedTest.test3();
//synchronizedTest.test4();
}
}
class SomeThing{
public synchronized void isSyncA(){
try{
for(int i=0 ; i<5 ; i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+" : isSyncA");
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
public synchronized void isSyncB(){
try{
for(int i=0 ; i<5 ; i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+" : isSyncB");
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
public static synchronized void cSyncA(){
try{
for(int i=0 ; i<5 ; i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+" : cSyncA");
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
public static synchronized void cSyncB(){
try {
for(int i=0 ; i<5 ; i++){
Thread.sleep(100);
System.out.println(Thread.currentThread().getName()+" : cSyncB");
}
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
作者:听说你很拽
链接:http://www.javaheidong.com/blog/article/207902/f4c3159eabcae991e1f1/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!