zoukankan      html  css  js  c++  java
  • 学习java第21天

    1.多线程同步

    class SyncCounter1
    {
     public static void main(String[] args){
      Num num = new Num();
      Thread counter1 = new Counter(num);
      Thread counter2 = new Counter(num);
      for( int i=0; i<10; i++ ){
       if(!num.testEquals()) break;
       try{                 
        Thread.sleep(100);
       }catch(InterruptedException e){
       }
      }
     }
    }
    class Num
    {
     private int x=0;
     private int y=0;
     void increase(){
      x++;
      y++;
     }
     boolean testEquals(){
      boolean ok = (x==y);
      System.out.println( x + "," + y +" :" + ok);
      return ok;
     }
    }
    class Counter extends Thread
    {
     private Num num;
     Counter( Num num ){
      this.num = num;
      this.setDaemon(true);
      this.setPriority(MIN_PRIORITY);
      this.start();
     }
     public void run(){
      while(true){
       num.increase();
      }
     }
    }
    2.同步
    java有互斥锁,保证共享数据操作的完整性
    关键字  synchronized  用来与对象的互斥锁联系
    *synchronized用法
    synchronized(对象){ ... }
    synchronized(this)
    class SyncCounter2
    {
     public static void main(String[] args){
      Num num = new Num();
      Thread counter1 = new Counter(num);
      Thread counter2 = new Counter(num);
      for( int i=0; i<10; i++ ){
       num.testEquals();
       try{                 
        Thread.sleep(100);
       }catch(InterruptedException e){
       }
      }
     }
    }
    class Num
    {
     private int x=0;
     private int y=0;
     synchronized void increase(){
      x++;
      y++;
     }
     synchronized boolean testEquals(){
      boolean ok = (x==y);
      System.out.println( x + "," + y +" :" + ok);
      return ok;
     }
    }
    class Counter extends Thread
    {
     private Num num;
     Counter( Num num ){
      this.num = num;
      this.setDaemon(true);
      this.setPriority(MIN_PRIORITY);
      this.start();
     }
     public void run(){
      while(true){
       num.increase();
      }
     }
    }
    3.线程的死锁
    class Worker
    {
     int id;
     public Worker(int id){ this.id=id; }
     synchronized void doTaskWithCooperator(Worker other){
      try{ Thread.sleep(500); } catch(Exception e){}
      synchronized(other){
       System.out.println("doing" + id);
      }
     }
    }
    class DeadLockDemo{
     public static void main(String[] args) {
      Worker w1 = new Worker(1);
      Worker w2 = new Worker(2);
            Thread td1 = new Thread(()->{
       w1.doTaskWithCooperator(w2);
      });
            Thread td2 = new Thread(()->{
       w2.doTaskWithCooperator(w1);
      });
      td1.start();
      td2.start();
        }
    }
     
     
    今天遇到的困难:对经典题型  “生产者-消费者问题”的不解
     
    明天要学习的内容:流式操作和并行流
  • 相关阅读:
    QT开发之旅一DS7400主机调试工具
    读《程序员,你伤不起》杂感(附带分享两个项目源码)
    这些年过上幸福生活的程序员(中篇)
    这些年过上幸福生活的程序员(上篇)
    如果第三方数据表与系统数据库里的表名格式不一致的解决方案
    数据库设计原则
    MYSQL密码设置
    关于phpmyadmin #1045无法登陆服务器的问题
    TP快捷函数
    跨控制器调用
  • 原文地址:https://www.cnblogs.com/SirNie/p/13380871.html
Copyright © 2011-2022 走看看