Five Java-related face questions per day (7)-Thread

Source: Internet
Author: User
Tags thread class

First of all, the thread is able to read my previous blog. Familiar with the thread life cycle diagram and text description

Good to begin with:

There are several ways to implement a thread in 1.java?

A: There are two ways to implement this. Each is inheriting the thread class and implementing the Runnable interface.

1. Thread classes inherit the syntax of the thread class:

(publicclass 类名 extends Thread{        publicvoidrun(){//这里写线程的内容}}

2. Thread classes implement the syntax of the Java.lang.Runnable class:

(publicclass 类名 implements Runnable{        publicvoidrun(){//这里写线程的内容}}
What is the difference between 2.sleep () and wait ()?

For:
Sleep is a thread-class method that causes this thread to pause for a specified time and give the opportunity to another thread, but the monitoring State remains. The time will be self-active recovery. Calling sleep does not release the object lock.


Wait is a method of the object class. Calling the wait method on this object causes this thread to discard the object lock, enters the waiting lock pool waiting for this object, and only has the Notify method (or Notifyall) issued against this object before the thread enters the object lock pool ready to get the object lock into the running state.

3. When a thread enters an synchronized method of an object, does another thread have access to other methods of this object?

For:
No, an synchronized method of an object can only be visited by a thread.

4. Please say the thread synchronization method you know.

For:
Wait (): causes a thread to be in a wait state. and release the lock of the object held.
Sleep (): causes a running thread to sleep. is a static method that calls this method to catch the interruptedexception exception.


Notify (): Wakes up a waiting thread, noting that when this method is called, it does not actually wake up a thread waiting for a state, but the JVM determines which thread to wake. And not by priority.


Allnotity (): Wakes all the threads into the waiting state. Note the lock is not an object that is given to all wake threads. But to let them compete.

5. What are the similarities and differences between synchronous and asynchronous? Under what circumstances are they used separately? An example is described.

For:
Assume that data is shared between threads. For example, the data being written may be read by another thread. Or the data being read may have been written by another thread, then this data is shared data. Synchronous access is required.


When an application invokes a method on an object that takes a very long time to run, and does not want the program to wait for the method to return. Asynchronous programming should be used, and in very many cases the asynchronous approach is often more efficient.

Five Java-related face questions per day (7)-Thread

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.