Java thread surface question Top 50 (reprint)

Source: Internet
Author: User

Original link: http://www.importnew.com/12773.html

Here's a nice typography: http://www.cnblogs.com/dolphin0520/p/3958019.html

Whether you are a new programmer or a veteran, you must have encountered a thread-related problem in your interview. An important feature of the Java language is the built-in support for concurrency , which makes Java popular with businesses and programmers. Most well-paid Java development positions require developers to be proficient in multithreaded technology and have extensive experience in Java program development , debugging , and optimization , so thread-related issues are often mentioned in interviews.

In a typical Java interview, the interviewer asks the basic concepts of the thread, such as: why you need to use threads, how to create threads, and how to create threads better (e.g., inheriting the thread class or calling the Runnable interface)

Note:

1. Implementing the Runnable interface allows you to inherit more of the other classes, which is better than inheriting the thread.

2. Inherit thread, multithreading is the instance of new multiple subclasses, implement Runnable, is the new one implements the Runnable instance, creates the multiple thread instance, the Runnable instance passes in.

In most cases, you should use the Runnable interface if you only want to override the run () method without overriding the other Thread methods. This is important because a subclass should not be created for the class (Thread) unless the programmer intends to modify or enhance the basic behavior of the class.

In this way, multiple thread instances share the data of the runnable instance. ( but strongly do not recommend runnable instance inside data, synchronization mechanism is not perfect, also need to consider synchronous )

Refer to Here, the Run method can also be added synchronized, indicating that the object is locked.

See Example:

classMyrunnableImplementsRunnable {intx = 5; @Override Public voidrun () { for(inti=0; i<5; i++) {System.out.println ("HI" + thread.currentthread (). GetName () + ":" + x--); }    }} Public classMain { Public Static voidMain (string[] args)throwsinterruptedexception {myrunnable Mr=Newmyrunnable (); Thread T1=NewThread (MR, "1"); Thread T2=NewThread (MR, "2"); Thread T3=NewThread (MR, "3");        T1.start ();        T2.start ();        T3.start ();    System.out.println (); }}

Results:

Hi 1:52:41:31:11:03:-13:-33:-43:-5  3:-62:22:-72:-82:-91:-2

Found to be chaotic.

And if you add synchronized to the Run method, the following:

 Public synchronized void  1:51:41:31:21:12:02:-12:-2  2:-32:-43:-53:-63:-73:-83:-9

As you can see, thread 1 prints 5 first, and then thread 2 prints. Because of the lock.

And then gradually asked what concurrency problems were like in Java concurrency programming, Java memory model, JDK1.5 introduced higher-order concurrency tools, concurrent programming common design patterns, classic multithreading problems such as producer consumers, philosophers dining, A reader or a simple bounded buffer problem. Just knowing the basic concept of threading is far from enough, you must know how to deal with the concurrency problems such as deadlock, race condition, memory conflict and line Cheng. With these tips, you can easily handle multithreading and concurrent interviews.

1) What is a thread?

A thread is the smallest unit that the operating system can perform operations on, which is included in the process and is the actual operating unit of the process. Programmers can use it for multiprocessor programming, and you can speed up operations-intensive tasks using multithreading. For example, if a thread takes 100 milliseconds to complete a task, it takes 10 milliseconds to complete the task with 10 threads. Java provides excellent support for multithreading at the language level, and it is also a good selling point.

2) What is the difference between threads and processes?

A thread is a subset of processes, a process can have many threads, and each thread performs different tasks in parallel. Different processes use different memory spaces, and all threads share the same amount of memory space. Don't confuse it with stack memory, each thread has a separate stack of memory to store local data.

The thread process further distinguishes the reference here: http://www.cnblogs.com/charlesblc/p/6135666.html

3) How do I implement threads in Java?

Java thread surface question Top 50 (reprint)

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.