静态同步方法(static synchronizd)

Java语法规定,任何线程进入同步方法、同步代码块之前,必须先获取同步方法、同步代码块对应的同步监视器

对于同步代码块而言,程序必须为它显示的指定同步监视器(可为this也可以自定义Object类型的全局变量);对于同步非静态方法而言,该方法的同步

监视器是this----即调用该方法的java对象;对于静态的同步方法而言,该方法的同步监视器不是this而是该类本身


public class SynchronizedStatic implements Runnable {

	 static boolean staticFlag = true;
	
	public static synchronized void test0(){
		for(int i=0;i<5;i++){
			System.out.println("test0:"+Thread.currentThread().getName() + " "+ i);
		}
	}
	public void test1(){
		synchronized (this) {
//		synchronized (SynchronizedStatic.class) {
			for(int i=0;i<5;i++){
				System.out.println("test1:"+Thread.currentThread().getName() + " "+ i);
			}
		}
	}
	
	public void run() {
		if(staticFlag){
			staticFlag = false;
			test0();
		}else{
			staticFlag = true;
			test1();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		SynchronizedStatic ss = new SynchronizedStatic();
		new Thread(ss).start();
		//保证第一条线程开始运行
//		Thread.sleep(1);
		new Thread(ss).start();
	}
}


执运行效果如下:

test0:Thread-0 0
test0:Thread-0 1
test1:Thread-1 0
test1:Thread-1 1
test1:Thread-1 2
test1:Thread-1 3
test1:Thread-1 4
test0:Thread-0 2
test0:Thread-0 3
test0:Thread-0 4

显然是并发执行,并没有达到同步执行(顺序执行)的效果。

       


	public void test1(){
//		synchronized (this) {
		synchronized (SynchronizedStatic.class) {
			for(int i=0;i<5;i++){
				System.out.println("test1:"+Thread.currentThread().getName() + " "+ i);
			}
		}
	}

执行结果如下:

test0:Thread-0 0
test0:Thread-0 1
test0:Thread-0 2
test0:Thread-0 3
test0:Thread-0 4
test1:Thread-1 0
test1:Thread-1 1
test1:Thread-1 2
test1:Thread-1 3
test1:Thread-1 4                                                 

,从而达到了线程同步的效果。

类似于Java并发编程:synchronized 中的

三.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
public  class  Test {
 
     public  static  void  main(String[] args)  {
         final  InsertData insertData =  new  InsertData();
         new  Thread(){
             @Override
             public  void  run() {
                 insertData.insert();
             }
         }.start(); 
         new  Thread(){
             @Override
             public  void  run() {
                 insertData.insert1();
             }
         }.start();
     }  
}
 
class  InsertData { 
     public  synchronized  void  insert(){
         System.out.println( "执行insert" );
         try  {
             Thread.sleep( 5000 );
         catch  (InterruptedException e) {
             e.printStackTrace();
         }
         System.out.println( "执行insert完毕" );
     }
     
     public  synchronized  static  void  insert1() {
         System.out.println( "执行insert1" );
         System.out.println( "执行insert1完毕" );
     }
}


  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用 Nacos 官方提供的 Java SDK 和 Nacos 配置客户端来读取 Nacos 配置中心中的配置。假设你已经引入了 Nacos 客户端依赖,可以按照以下步骤进行静态方法读取: 1. 创建 Nacos 配置客户端 ```java import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; public class NacosConfigClient { private static final String SERVER_ADDR = "localhost:8848"; // Nacos 服务器地址 private static final String DEFAULT_GROUP = "DEFAULT_GROUP"; // 配置分组 private static final long TIMEOUT_MS = 5000; // 请求超时时间 private static ConfigService configService; static { try { configService = NacosFactory.createConfigService(SERVER_ADDR); } catch (NacosException e) { e.printStackTrace(); } } public static ConfigService getConfigService() { return configService; } } ``` 2. 读取配置 ```java import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; public class NacosConfigUtil { private static final String DATA_ID = "example"; // 配置 ID public static String getConfig() { try { ConfigService configService = NacosConfigClient.getConfigService(); return configService.getConfig(DATA_ID, DEFAULT_GROUP, TIMEOUT_MS); } catch (NacosException e) { e.printStackTrace(); } return null; } } ``` 在上面的示例中,我们通过 `NacosConfigClient` 类的静态方法获取了一个 Nacos 配置客户端实例,并在 `getConfig()` 方法中使用该客户端来读取指定配置 ID 的配置内容。你只需要将 `DATA_ID` 修改为你自己的配置 ID 即可使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值