zoukankan      html  css  js  c++  java
  • 多线程synchronized锁

    11、

    Synchronized使用方式
    代码块形式
    1)、使用到Synchronized锁:任务对象锁
    2)、方法(非静态)默认this锁
    3)、方法(静态),当前类Class字节码锁

    i)、this或object

    public class TickerThread implements Runnable {

    private int count=100;

    @Override
    public void run() {
    while (count>0){
    ticket();
    }
    }


    Object object=new Object();
    public void ticket(){

    //synchronized(object){
    synchronized(this){
    if(count>0){
    try {
    Thread.sleep(30);
    }catch (Exception e)
    {

    }
    System.out.println(Thread.currentThread().getName()+",当前系统余票【"+count+"】");
    System.out.println(Thread.currentThread().getName()+",正在出票第【"+(100-count+1)+"】张");
    count--;
    if(count==0)
    {
    System.out.println(Thread.currentThread().getName()+",当前系统的票已售尽,当前系统余票【"+count+"】");
    }
    }
    }

    }

    public static void main(String arg[])
    {
    TickerThread tickerThread=new TickerThread();
    new Thread(tickerThread,"售票机1号").start();
    new Thread(tickerThread,"售票机2号").start();
    new Thread(tickerThread,"售票机3号").start();
    new Thread(tickerThread,"售票机4号").start();
    new Thread(tickerThread,"售票机5号").start();
    }

    }

    ii)、非静
    public class TickerThread implements Runnable {

    private int count=100;

    @Override
    public void run() {
    while (count>0){
    ticket();
    }
    }

    public synchronized void ticket(){
    if(count>0){
    try {
    Thread.sleep(30);
    }catch (Exception e)
    {

    }
    System.out.println(Thread.currentThread().getName()+",当前系统余票【"+count+"】");
    System.out.println(Thread.currentThread().getName()+",正在出票第【"+(100-count+1)+"】张");
    count--;
    if(count==0)
    {
    System.out.println(Thread.currentThread().getName()+",当前系统的票已售尽,当前系统余票【"+count+"】");
    }
    }
    }

    public static void main(String arg[])
    {
    TickerThread tickerThread=new TickerThread();
    new Thread(tickerThread,"售票机1号").start();
    new Thread(tickerThread,"售票机2号").start();
    new Thread(tickerThread,"售票机3号").start();
    new Thread(tickerThread,"售票机4号").start();
    new Thread(tickerThread,"售票机5号").start();
    }

    }


    iii)、静态
    public class TickerThread implements Runnable {

    private static int count=100;

    @Override
    public void run() {
    while (count>0){
    ticket();
    }
    }

    public static void ticket(){
    synchronized(TickerThread.class){
    if(count>0){
    try {
    Thread.sleep(30);
    }catch (Exception e)
    {

    }
    System.out.println(Thread.currentThread().getName()+",当前系统余票【"+count+"】");
    System.out.println(Thread.currentThread().getName()+",正在出票第【"+(100-count+1)+"】张");
    count--;
    if(count==0)
    {
    System.out.println(Thread.currentThread().getName()+",当前系统的票已售尽,当前系统余票【"+count+"】");
    }
    }
    }

    }

    public static void main(String arg[])
    {
    TickerThread tickerThread=new TickerThread();
    new Thread(tickerThread,"售票机1号").start();
    new Thread(tickerThread,"售票机2号").start();
    new Thread(tickerThread,"售票机3号").start();
    new Thread(tickerThread,"售票机4号").start();
    new Thread(tickerThread,"售票机5号").start();
    }

    }

  • 相关阅读:
    Confluence无法打开编辑器,一直在转圈
    Xamarin.Forms中的ListView的ItemTrapped事件与ItemSelected事件的区别
    C#读取物理网卡信息及其对应的IP地址
    【Xamarin报错】visual studio android 模拟器部署卡住
    【Xamarin报错】AndroidManifest.xml : warning XA0101: @(Content) build action is not supported
    【Xamarin报错】 COMPILETODALVIK : UNEXPECTED TOP-LEVEL error java.lang.OutOfMemoryError: Java heap space
    【Xamarin报错】libpng warning : iCCP: Not recognizing known sRGB profile that has been edited
    子窗口调用父窗口
    Windows Phone 8.1 多媒体(3):音乐
    Windows Phone 8.1 多媒体(1):相片
  • 原文地址:https://www.cnblogs.com/smallfa/p/14578424.html
Copyright © 2011-2022 走看看