Java Threading and concurrency programming Practices (i)

Source: Internet
Author: User

first, the definition of the outgoing thread

A thread is a path that executes independently in the program code

Second, give the definition of runnable

A runnable is a sequence of code encapsulated in an object, and its class implements the Runnable interface

Iii. what does the thread class and runnable interface accomplish?

The class thread provides a unified interface for the threading architecture of the underlying operating system. The Runnable interface provides execution code for threads that have thread objects associated with them.

Iv. indicate two ways to create a Runnable object?

Create an anonymous class or lambda expression that implements the Runnable interface

Runnable r = new Runnable () {///anonymous class, implement Runnable interface @overridepublic void Run () {System.out.println ("MyThread"); }};


Runnable r = ()-System.out.println ("MyThread"); Lambda expression

V. Indicate two ways of associating a runnable to a thread object?

A. Passing a Runnable object into the thread's constructor

B. Inherit the thread class to rewrite its run () method because the thread class also implements the Runnbale interface

Vi. indicate the status of 5 thread types

The thread name, the identity of the thread's survival, the execution state of the thread, the priority of the thread, whether the thread is a daemon thread

Will the name of the default thread be prefixed with thd-?

No, with thread-as the prefix

Viii. How to give a non-default name to a thread?

You can pass in through the thread's constructor, or you can call the SetName () method to specify a name

Nine, how to determine whether the thread is dead or alive?

Use the IsAlive () method to determine

X. Constants that indicate the Thread.state enumeration

NEW: The status thread has not yet started executing

RUNNABLE: The status thread is executing in the JVM

BLOCKED: The status of the thread is blocked, waiting for a listening lock

Waiting: The status thread waits indefinitely for a specific operation to be performed by another

TERMINATED: The status thread has exited

Xi. How to get the execution state of the current thread

Call thread.state or call the GetState () method

12, the definition of the priority level

Priority refers to the relative importance of the thread

13. How can I use SetPriority () to influence the portability of applications across operating systems?

Using SetPriority () can affect the portability of applications between operating systems, because different schedulers handle priorities in different ways

14. Determine the range of values for the void setpriority (int priority) parameter of the thread

Between Thread.min_priority and Thread.max_priority

After the last non-daemon thread of the application dies, the daemon will automatically die to allow the application to exit

That's right

16. What happens to the Void start () method that calls thread on a running or dead thread object?

Will throw Java.lang.IllegalThreadStateException

17. How to terminate a program on Windows that cannot be stopped

CTRL + C

18. All methods that make up the thread interrupt mechanism

Interrupt ()

Interrupted ()

Isinterrupted ()

19, Isinterrupted () method clears the interrupt state of the thread

Error, does not affect

20. How is the thread reflected when the thread is interrupted?

Throw Interruptedexception

21, give the definition of the busy cycle

A looping statement designed to consume time

22. The method that lets one thread wait for another thread to die

Join ()

23. The thread method that lets you sleep

Sleep ()

24. Write an application called Intsleep, which creates a background thread, continuously prints out Hello, and then sleeps 100ms. After sleeping 2s, the default main thread should interrupt the background thread, which jumps out of the loop after printing out the interrupted

public class Intsleep {public static void main (string[] args) {Runnable r = new Runnable () {@Overridepublic void run () {WH Ile (True) {System.out.println ("Hello"), try {thread.sleep,} catch (Interruptedexception e) {System.out.println (" Interrupted "); break;}}}; Thread t = new Thread (r); T.start (); try {thread.sleep (n);} catch (Interruptedexception e) {}t.interrupt ()}}


Java Threading and concurrency programming Practices (i)

Related Article

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.