1 Star 1 Fork 0

ChenforCode/synchronized

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
SynchronizedObjectMethod3.java 928 Bytes
Copy Edit Raw Blame History
/**
* @author: ChenforCode
* @date: 2019/4/9 18:14
* @description: 方法对象锁
*/
public class SynchronizedObjectMethod3 implements Runnable{
private static SynchronizedObjectMethod3 instance = new SynchronizedObjectMethod3();
@Override
public void run() {
method();
}
public synchronized void method (){
System.out.println("我是对象锁的方法修饰符形式,名字是:" + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "运行结束");
}
public static void main(String[] args){
Thread t1 = new Thread(instance);
Thread t2 = new Thread(instance);
t1.start();
t2.start();
while (t1.isAlive() || t2.isAlive()){
}
System.out.println("finish");
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/chenforcode/synchronized.git
git@gitee.com:chenforcode/synchronized.git
chenforcode
synchronized
synchronized
master

Search