java 字符串锁
package com.example.demo.controller; public class StringLock { public void method(String p) { // new String("字符串常量") synchronized (p.intern()) { try { System.out.println("当前线程 : " + Thread.currentThread().getName() + "开始"); Thread.sleep(3000); System.out.println("当前线程 : " + Thread.currentThread().getName() + "结束"); } catch (InterruptedException e) { e.printStackTrace(); } } } public static void main(String[] args) { Thread t1 = new Thread(new Runnable() { @Override public void run() { StringLock stringLock = new StringLock(); stringLock.method("123"); } }, "t1"); Thread t2 = new Thread(new Runnable() { @Override public void run() { StringLock stringLock1 = new StringLock(); stringLock1.method("123"); } }, "t2"); t1.start(); t2.start(); } }