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

How to share data among Java multithreads

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to share data between Java multithreads, which is very detailed and has certain reference value. Friends who are interested must finish reading it.

1. Thread-wide shared variables

Multiple business modules operate on the same static variable to ensure that each module operates its own corresponding variable object in different threads.

Public class ThreadScopeSharaData {private static int data = 0; public static void main (String [] args) {for (int I = 0; I 0) {ticket--; System.out.println ("current votes:" + ticket);}

(2) if each thread executes different code, you need to use different Runnable objects, for example, design 2 threads. One thread adds 1 to j and the other to j minus 1, the bank deposit and withdrawal system.

Public class MultiThreadShareData {private int j; public static void main (String [] args) {MultiThreadShareData multiThreadShareData = new MultiThreadShareData (); for (int iTuno [)

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

Development

Wechat

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

12
Report