Java thread-in-the-middle range sharing issues

Source: Internet
Author: User

This baby novice, do not spray! Directly on the code,

Thread-wide sharing issues: sharing the same block of data between each thread, one data corruption is all damaged, you need to run a bit!

public class Threads {
private static Hashmap<thread, integer> data = new hashmap<thread,integer> ();
public static void Main (string[] args) {
for (int i = 0; i < 2; i++) {
New Thread (New Runnable () {

@Override
public void Run () {
int value = new Random (). Nextint (1000);
Data.put (Thread.CurrentThread (), value);
System.out.println ("name is:" +thread.currentthread (). GetName () + "data is:" +value);
A a1 = new A ();
A1.getdata ();
b B1 = new B ();
B1.getdata ();
c C = new C ();
C.getdate ();
}
}). Start ();
}
}
Static Class a{
public void GetData () {
Thread thread = Thread.CurrentThread ();
int value = Data.get (thread);
System.out.println (Thread.getname () + "get data from a" +value);
}
}
Static Class b{
public void GetData () {
Thread thread = Thread.CurrentThread ();
int value = Data.get (thread);
System.out.println (Thread.getname () + "Get data from B" +value);
}
}
Static Class c{
public void GetDate () {
Thread thread = Thread.CurrentThread ();
int value = Data.get (thread);
System.out.println (Thread.getname () + "get data from C" +value);
}
}

}

Java thread-in-the-middle range sharing issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.