Java Multithreading Basics (i)

Source: Internet
Author: User

1, there are two ways to create a thread, one way is to use the method of inheriting the thread class to create a thread class, you cannot share the thread class's instance variables between multiple threads.  Because the program needs to create a thread object each time the thread object is created, the instance properties cannot be shared. Alternatively, multiple threads created in a way that implements the Runnable interface or callable interface can share instance properties of the thread class. This is because, in this way, the Runnable object created by the program is only the target of the thread, and multiple threads can share the same target, so multiple threads can share instance properties of the same thread class (which should actually be the target class of the thread).

(1) Create multithreading in a way that implements Runnable, callable interfaces:

The thread class simply implements the Runnable interface or callable interface and can inherit other classes.

In this way, multiple threads can share the same target object, so it is very suitable for multiple identical threads to handle the same resource, so that the CPU, code and data can be separated to form a clear model, which embodies the idea of object-oriented.

The disadvantage is that programming is slightly more complex, and if you need access to the current thread, you must use the Thread.CurrentThread () method.

(2) Create multithreading in a way that inherits the thread class.

The disadvantage is that because the thread class has inherited the thread class, it is no longer possible to inherit other parent classes.

The advantage is that writing is simple and if you need access to the current thread, you do not need to use the Thread.CurrentThread () method to get the current thread directly using this.

It is generally recommended to implement Runnable interface, callable interface way to create multithreading.

2. When the main thread ends, other threads are unaffected and will not end with it. Once the child thread starts up, he has the same status as the main thread, and he is not affected by the main thread.

3. Do not call the start () method on a thread that is in a dead state, the program can only call the start () method on a new state thread, and the start () method two times for a new state thread is also wrong. This will cause illegalthreadstateexception exceptions.

4, the yield () method is a bit similar to the sleep () method, he is also the thread class provides a static method, he can also let the currently executing thread paused, but he does not block the thread, he just put the thread into a ready state.

5. The difference between the sleep () method and the yield () method is as follows:

When the sleep () method pauses the current thread, it gives the opportunity for other threads to execute, regardless of the priority of the other thread, but the yield () method only gives the same priority or higher priority thread execution opportunity.

The sleep () method moves the thread into a blocking state until it is blocked until it goes into a ready state, and yield () does not put the thread into a blocking state, he simply forces the current thread into a ready state. It is therefore entirely possible that once a thread calls the yield () method to pause, it immediately gets the processor resource again to be executed.

The sleep () method declaration throws a interruptedexception exception, so calling the sleep () method either catches the exception or the declaration throws the exception, and the yield () method does not declare any exceptions.

The sleep () method is more portable than the yield () method, and it is generally not recommended to use the yield () method to control the execution of concurrent threads.

Java Multithreading Basics (i)

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.