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();
    }

    }

  • 相关阅读:
    2015/8/3 接着跌
    2015/7/31 由于昨天上升缺乏量的支持,今天横盘;在箱体下边缘稍微买了一点---错误!;复文《揭秘主力坐庄流程 内幕超乎想象》,
    打包jar类库与使用jar类库
    java eclipse 监视选择指定变量
    2015/7/29 (高开,V形反转,各种指标背离——可惜没买进,填补空缺图形的心理分析)
    XP、win7下Excel 2007多窗口打开Excel的解决方法
    2015/7/28(总结昨天抄底操作失败-割肉自保)
    六首失传股诗教你如何抄底和逃顶
    2015/7/27 (主力流出-1200亿,上周五回踩,今天到底是震荡下行,还是红魔呢?——在周五成功逃顶,结果今天回调的时候被套!——教训!)
    java中byte数组与int,long,short间的转换
  • 原文地址:https://www.cnblogs.com/smallfa/p/14578424.html
Copyright © 2011-2022 走看看