Synchronized的用法
来源:互联网 发布:python获取当天日期 编辑:程序博客网 时间:2024/09/21 06:33
Java中Synchronized的用法:
synconized:举个例子就能知道什么意思
火车站买票程序,比如说有10张票开了4个窗口(分别为A,B,C,D)同时买票,应该是A卖出一张剩9张,B卖出一张剩8张,张数应该没卖出一张减一,直到为0,但是如果不适用synconized,会出现什么情况呢?不使用synconized关键字,A卖出一张剩9张,B卖出一张有可能还剩9张,但是这不是我们需要的,接下来就是我们来了解synconized啦!
1.同步方法
public class NewRunnable implements Runnable{int a=10;
public void run() {
method();
}
public synchronized void method(){
while (a>=0) {
System.out.println(Thread.currentThread().getName()+"=="+a);
a--;
}
}
}
调用方法:(1):卖票程序正常
public class MianTest {
public static void main(String[] args) {
NewRunnable newRunnable=new NewRunnable();
Thread thread1=new Thread(newRunnable,"thread1");
Thread thread2=new Thread(newRunnable,"thread2");
thread1.start();
thread2.start();
}
}
(2):卖票程序不正常
public class MianTest {
public static void main(String[] args) {
Thread thread1=new Thread(new NewRunnable(),"thread1");
Thread thread2=new Thread(new NewRunnable(),"thread2");
thread1.start();
thread2.start();
}
}
总结:必须使用同一个NewRunnable对象
2同步代码块
public class NewRunnable implements Runnable{
int a=10;
public void run() {
synchronized (this) {
while (a>=0) {
System.out.println(Thread.currentThread().getName()+"=="+a);
a--;
}
}
}
}
调用方法:(1):卖票程序正常
public class MianTest {
public static void main(String[] args) {
NewRunnable newRunnable=new NewRunnable();
Thread thread1=new Thread(newRunnable,"thread1");
Thread thread2=new Thread(newRunnable,"thread2");
thread1.start();
thread2.start();
}
}
(2):卖票程序不正常
public class MianTest {
public static void main(String[] args) {
Thread thread1=new Thread(new NewRunnable(),"thread1");
Thread thread2=new Thread(new NewRunnable(),"thread2");
thread1.start();
thread2.start();
}
}
3.静态同步方法
public class NewRunnable implements Runnable{
static int a=10;
public void run() {
method();
}
public static synchronized void method(){
while (a>=0) {
System.out.println(Thread.currentThread().getName()+"=="+a);
a--;
}
}
}
调用方法:(1):卖票程序正常
public class MianTest {
public static void main(String[] args) {
NewRunnable newRunnable=new NewRunnable();
Thread thread1=new Thread(newRunnable,"thread1");
Thread thread2=new Thread(newRunnable,"thread2");
thread1.start();
thread2.start();
}
}
(2):卖票程序正常
public class MianTest {
public static void main(String[] args) {
Thread thread1=new Thread(new NewRunnable(),"thread1");
Thread thread2=new Thread(new NewRunnable(),"thread2");
thread1.start();
thread2.start();
}
}
4.synchronized同步类
public class NewRunnable implements Runnable{static int a=10;
public void run() {
synchronized (NewRunnable.this) {
while (a>=0) {
System.out.println(Thread.currentThread().getName()+"=="+a);
a--;
}
}
}
}
调用:
(1)不正常显示
public class MianTest {
public static void main(String[] args) {
Thread thread1=new Thread(new NewRunnable(),"thread1");
Thread thread2=new Thread(new NewRunnable(),"thread2");
thread1.start();
thread2.start();
}
}
(2)正常显示
public class MianTest {
public static void main(String[] args) {
NewRunnable newRunnable=new NewRunnable();
Thread thread1=new Thread(newRunnable,"thread1");
Thread thread2=new Thread(newRunnable,"thread2");
thread1.start();
thread2.start();
}
}
总结:同步类时,必须使用同一个对象。
总结:对象调用时,必须使用同一个对象;当同步方法是静态方法时,可以用类直接调用。
- synchronized的用法
- synchronized的用法
- synchronized的用法
- synchronized的用法
- synchronized的用法
- synchronized的用法
- synchronized的用法简介
- synchronized的用法
- synchronized的用法汇总
- synchronized关键字的用法
- java的synchronized用法
- synchronized的用法
- @synchronized(self)的用法
- Synchronized的用法
- 【转载】synchronized的用法
- synchronized关键字的用法
- Java-synchronized的用法
- synchronized的用法
- 自定义PopupWindow并且设置背景色变暗的2种方法(蒙层效果)
- ClipDrawable资源之徐徐展开的风景
- LinkedHashMap和HashMap的区别以及使用方法
- Activity 生命周期与启动模式
- Oracle group up函数与rollup的使用说明
- Synchronized的用法
- ios下iframe内的加载更多为什么失效了?
- android开发中,可能会导致内存泄露的问题
- 【Unity】 C# 协程 WaitForSeconds产生GC(Garbage Collection)问题
- ssh的前世今生
- JD-Eclipse 安装
- 强制跳转HTTPS
- 宏函数的注意事项
- python截屏