Java multithreading,

Source: Internet
Author: User

Java multithreading,
11.1) multithreading concept 11.2) 2 ways to create multithreading 11.3) multithreading lifecycle 11.4) Why synchronization is required?

Because of the thread security issue, for example, the bank's money Acquisition problem, the multi-thread for the same bank account to get the money task, the account will have negative assets, not in line with the real life

Therefore, when multiple threads access the same resource, it is easy to encounter security problems.

What is synchronization?

To avoid security problems, you can synchronize

Two Synchronization Methods

The first is the synchronized code block.

Synchronized {// code to be protected ...}

  

The second method is synchronization (modified with the synchronized keyword)

public synchronized void func(){      ....  } 

  

ThreadLocal class

ThreadLocal thread local variables support generics. By using thread local variables, you can avoid "Security Issues" when multiple threads access the same resource ", the method is to copy multiple copies of the same resource for multithreading. That is to say, each thread has a copy variable during access.

ThreadLocal only provides three public methods:

T get (): return the value void remove () in the current thread copy in the local variable of this thread: Delete the value void set (T value) in the current thread copy of this thread local variable ): set the value of the current thread copy in the local variable of this thread

Use:

1. class Account {2. /** 3. * define a ThreadLocal variable, which is a local variable of the thread type. 4. * Each thread retains a copy of the variable. */6. private ThreadLocal <String> name = new ThreadLocal <> (); 7. // define a constructor that initializes the name member variable. public Account (String name) {9. this. name. set (name); 10. // The following code is used to access the current thread's name copy value 11. system. out. println ("-----" + this. name. get (); 12 .} 13. // getter and setter methods for name 14. public String getName () {15. return this. name. get (); 16 .} 17. public void setName (String name) {18. this. name. set (name); 19 .} 20 .} 21. class MyThread extends Thread {22. // define a member variable of the Account type 23. private Account account; 24. public MyThread (Account acct, String name) {25. super (name); 26. this. account = acct; 27 .} 28. 29. public void run () {30. 31. for (int I = 0; I <10; I ++) {32. // when I = 6, the output replaces the account name with the current thread name 33. if (I = 6) {34. account. setName (getName (); 35 .} 36. // output the account name and circular variable of the same account 37. system. out. println (account. getName () + "account I value:" + I); 38 .} 39 .} 40 .} 41. public class ThreadLocalTest {42. public static void main (String [] args) {43. // start two threads and share the same Account 44. account acct = new Account ("initial name"); 45. /** 46. * Although two threads share the same account, there is only one account named 47. * Because the account name is a ThreadLocal class, each thread is 48. * All have copies of their respective account names, so after I = 6, you will see 49. * When two threads access the same account, 50 different account names appear. */51. new MyThread (acct, "thread "). start (); 52. new MyThread (acct, "thread B "). start (); 53 .} 54 .}

  

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.