Dark Horse Programmer--some summary of threads

Source: Internet
Author: User

1. Why do I overwrite the run () method?

The thread class is used to describe threads. The class defines a function that stores the code that the thread wants to run, and the function of the store is implemented by the run () method.

2. The running state of the thread:

The thread is created, the start () method is called to enter the running state, and if the call to sleep (time) or the Wait () method thread enters a frozen state, but still exists, you can try the Notify () method to wake the thread;

When the thread finishes running or calls the Stop () method, the thread dies.

3. Security issues with multithreading

WORKAROUND: A statement that shares data on multiple operations can only be done after one thread has finished executing, and other threads are not allowed to participate during execution.

YoY code block: Synchronized (object) {code to synchronize}

The above object is actually a lock, the thread that holds the lock can execute code in the synchronous code block, the thread that does not hold the lock even gets the CPU execution right, because the lock object is not acquired

Prerequisites for synchronization: Two or more than two threads

Multiple threads must use the same lock object

4. How do I see which code needs to be synchronized?

  • Identify which code is multithreaded to run code
  • Identify what is shared data
  • Identify which statements in multithreaded run code work with shared data

Dark Horse Programmer--some summary of threads

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.