资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

如何解决Java多线程的临界资源问题

小编给大家分享一下如何解决Java多线程的临界资源问题,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网站空间、营销软件、网站建设、尉氏网站维护、网站推广。

临界资源问题的原因:某一个线程在对临界资源进行访问时,还没来得及完全修改临界资源的值,临界资源就被其他线程拿去访问,导致多个线程访问同一资源。直观表现为打印结果顺序混乱。

解决方法:加锁

静态方法中用类锁,非静态方法中用对象锁。

1.同步代码段:synchronized(){...}

2.同步方法:使用关键字synchronized修饰的方法

3.使用显式同步锁ReentrantLock

锁池描述的即为锁外等待的状态

方法一:同步代码段:synchronized(){...}

public class SourceConflict {
  public static void main(String[] args) {
    //实例化4个售票员,用4个线程模拟4个售票员
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        synchronized(" ") {
          if (TicketCenter.restCount <= 0) {
            return;
          }
          System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + --TicketCenter.restCount + "张票");
        }
      }
    };
    
    //用4个线程模拟4个售票员
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //开启线程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }  
}

//实现四名售票员共同售票,资源共享,非独立
//Lambda表达式或匿名内部类内部捕获的局部变量必须显式的声明为 final 或实际效果的的 final 类型,而捕获实例或静态变量是没有限制的
class TicketCenter{
  public static int restCount = 100; 
}

方法二:同步方法,即使用关键字synchronized修饰的方法

public class SourceConflict2 {
  public static void main(String[] args) {
    //实例化4个售票员,用4个线程模拟4个售票员
    
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        sellTicket();
      }
    };
    
    //用4个线程模拟4个售票员
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //开启线程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }
  
  private synchronized static void sellTicket() {  
    if (TicketCenter.restCount <= 0) {
      return;
    }
    System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + --TicketCenter.restCount + "张票");
  }
}

class TicketCenter{
  public static int restCount = 100; 
}

方法三:使用显式同步锁ReentrantLock

import java.util.concurrent.locks.ReentrantLock;

public class SourceConflict3 {
  public static void main(String[] args) {
    //实例化4个售票员,用4个线程模拟4个售票员
    
    //显式锁
    ReentrantLock lock = new ReentrantLock();
    Runnable r = () -> {
      while (TicketCenter.restCount > 0) {
        lock.lock();
        if (TicketCenter.restCount <= 0) {
          return;
        }
        System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + --TicketCenter.restCount + "张票");
        lock.unlock();
      }
    };
    
    //用4个线程模拟4个售票员
    Thread thread1 = new Thread(r, "thread-1");
    Thread thread2 = new Thread(r, "thread-2");
    Thread thread3 = new Thread(r, "thread-3");
    Thread thread4 = new Thread(r, "thread-4");
    
    //开启线程
    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    
  }  
}
class TicketCenter{
  public static int restCount = 100; 
}

看完了这篇文章,相信你对“如何解决Java多线程的临界资源问题”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


新闻标题:如何解决Java多线程的临界资源问题
标题来源:http://cdkjz.cn/article/gceppc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220