Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of locking blocks in Java thread safety

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of locking blocks in Java thread safety, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Java thread safety article. Let's take a look at it.

Public class Block {public static void main (String [] args) {SafeAccount safeaccount=new SafeAccount (100, "gifts"); SafeDrawing you= new SafeDrawing (safeaccount,80, "miserable"); SafeDrawing wife= new SafeDrawing (safeaccount,90, "happy"); you.start (); wife.start ();}} class SafeAccount {int money;String name;public SafeAccount (int money, String name) {this.money = money;this.name = name;}} class SafeDrawing extends Thread {SafeAccount safeaccount / the amount of money in the account (intended to be transferred to the Account object) the number of money withdrawn by int drawingMoney;// in int packetMoney;// pocket public SafeDrawing (SafeAccount safeaccount, int drawingMoney, String name) {super (name); this.safeaccount = safeaccount;this.drawingMoney = drawingMoney;} @ Override public void run () {test ();} public void test () {synchronized (safeaccount) {if (safeaccount.money-drawingMoney)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report