zoukankan      html  css  js  c++  java
  • 线程安全--- synchronized

    package com.Thread;
     
    class W12306 implements Runnable{
           private boolean flag = true;
           private int num =10;
           @Override
           public void run() {
                 while(flag ) {
                      test6();
                }
          }
          
           //线程不安全,锁定不正确,一般锁定对象
           public void test6() {
                 if(num <=0) {
                       flag = false ;
                       return;
                }
                 synchronized(this ) {
                       try {
                            Thread. sleep(500);//模拟网络延时,最后取到了0和-1
                      } catch (InterruptedException e) {
                            e.printStackTrace();
                      }
                      System. out.println(Thread.currentThread().getName() + "抢到了" + num--);
                }
     
          }
          
           //线程不安全,锁定资源不正确
           public void test5() {
                 synchronized((Integer)num ) {
                       if(num <=0) {
                             flag = false ;
                             return;
                      }
                       try {
                            Thread. sleep(500);//模拟网络延时,最后取到了0和-1
                      } catch (InterruptedException e) {
                            e.printStackTrace();
                      }
                      System. out.println(Thread.currentThread().getName() + "抢到了" + num--);
                }
     
          }
     
          
           //线程不安全,锁定范围过小
           //a b c
           public void test4() {
                 synchronized(this ) {
                       //b
                       if(num <=0) {
                             flag = false ;
                             return;
                      }
                }
                 //b
                 try {
                      Thread. sleep(500);//模拟网络延时,最后取到了0和-1
                } catch (InterruptedException e) {
                      e.printStackTrace();
                }
                System. out.println(Thread.currentThread().getName() + "抢到了" + num --);
                 //a-->1
     
          }
     
          
           //线程安全,锁定方法块
           public void test3() {
                 synchronized(this ) {
                       if(num <=0) {
                             flag = false ;
                             return;
                      }
                       try {
                            Thread. sleep(500);//模拟网络延时,最后取到了0和-1
                      } catch (InterruptedException e) {
                            e.printStackTrace();
                      }
                      System. out.println(Thread.currentThread().getName() + "抢到了" + num--);
                }
     
          }
     
          
           //线程安全,锁定方法
           public synchronized void test2() {
                 if(num <=0) {
                       flag = false ;
                       return;
                }
                 try {
                      Thread. sleep(500);//模拟网络延时,最后取到了0和-1
                } catch (InterruptedException e) {
                      e.printStackTrace();
                }
                System. out.println(Thread.currentThread().getName() + "抢到了" + num --);
     
          }
          
           //线程不安全
           public void test1() {
                 if(num <0) {
                       flag = false ;
                }
                 try {
                      Thread. sleep(500);//最后取到了0和-1
                } catch (InterruptedException e) {
                      e.printStackTrace();
                }
                System. out.println(Thread.currentThread().getName() + "抢到了" + num --);
     
          }
    }
    public class Synchronized_ {
           public static void main(String[] args) {
                W12306 w = new W12306();
                
                Thread t1 = new Thread(w, "甲" );
                Thread t2 = new Thread(w, "已" );
                Thread t3 = new Thread(w, "丁" );
                
                t2.start();
                t1.start();
                t3.start();
          }
    }
  • 相关阅读:
    [算法]外部排序
    [笔试]华为编程大赛题目
    [C++]字符串处理方法(STL与C风格)
    如何动态建立VFP能够打开的中文字段 dbf 表 北极星
    使用 VCL BDE 组件动态创建数据库表 北极星
    如何用Table控件判断数据库是否为空 北极星
    DNGuard HVM副产品(元数据名称编辑器)
    常见dotNet加密保护工具分析介绍
    DNGuard HVM 试用版 RC1 发布
    [转载]Modifying IL at runtime
  • 原文地址:https://www.cnblogs.com/king-/p/4389738.html
Copyright © 2011-2022 走看看