Java Interview 11th Day

Source: Internet
Author: User

Multithreading:

Processes and Threads:

Process: A subroutine executed in the same operating system, containing three parts of the virtual CPU, code, data

Multi-process: Multiple parallel sub-programs executed in the same operating system. can increase CPU usage

Threads: subroutine flow executing in the same process

Multithreading: Multiple subroutine flows that execute concurrently in the same process. can increase CPU usage

The difference between a process and a thread:

Processes have separate process space, and the data storage space (heap space and stack space) in the process is independent.

The heap space of the thread is shared, the stack space is independent, and the thread consumes less resources than the process, which can affect each other.

How to tune a process in Java:

Two classes calling the local program

Runtime

Runtime.getRuntime.exec (...); Execute a program

The return value is the process type

Process

Attention:

Only the running state thread has the opportunity to execute the code, and the abort of the main thread does not affect the other running threads, and the main thread abort is the main () method exiting. The process (JVM process) exits only if all threads in the process are aborted, and the process does not exit as long as the thread is not aborted.

The operating system determines whether a thread has priority, the system in an exclusive operating system has a priority concept, and the shared operating system does not have a priority.

Inside the thread, the program still executes sequentially

Two ways to thread programming:

Write a class that inherits the thread class, overwriting the run () method inherited from the thread class, so that the custom thread class is written.

Inherit the Java.lang.Thread class:

Class MyThread extends thread{

public void Run () {//overwrite run (), thread body method, itself is a common method

.......

}

}

To start a thread:

public class testthread{

public static void Main () {

Thread t1=new Mythread ();

T1.start (); Call Start () to start the thread, thread to start the method, to the thread scheduler to explain that the current thread is ready, is a running state

}

}

Write a class that implements the Runable interface, which implements the run () method. This method writes the arguments of the class object that need to be constructed as the thread class when the object is created.

Implement the Java.lang.Runnable interface:

Class MyThread implements runnable{

public void Run () {

}

}

To start a thread:

public class testthread{

public static void Main () {

Runnable myThread = new MyThread ();

Thread t = new thread (myThread);

T.start ();

}

}

Some of the methods in thread:

CurrentThread ()

Returns a reference to the currently executing thread object (used when implementing the interface mode)

Sleep (Long Millis)

Sleeps (suspends execution) the currently executing thread within the specified number of milliseconds.

This thread is not going to rob unless sleep ends.

Execution permissions are snatched between multiple threads, regardless of priority.

Yield ()

Pauses the currently executing thread object and executes other threads.

Only threads of this class or priority greater than this class are robbed.

Join ()

Waits for the thread to terminate.

It is useless to put it in front of start ().

Setdaemon (Boolean on)

Marking the thread as a daemon thread, the daemon needs to rely on other threads and will stop when the virtual machine stops.

The life cycle of a thread:

1) Initial state: At this point the thread is just in the JVM process, just creating a thread object and not really starting to run.

2) Movable line state: invokes the Start () method of the Thread object, at which point the thread is actually created, enters the operational state, waits for the CPU to dispatch. "Everything is ready, only owed to the CPU."

3) Running state: The running thread, at which point it has the CPU execution right.

4) Blocking state: The thread in the running state, if it is waiting for the user to input or call the sleep () and join () and other methods will cause the thread to enter the blocking state, note that the thread from the blocking state is not immediately back to the running state, but back to the operational state, waiting for the CPU to dispatch again.

5) Wait queue status: A thread calls an object's wait () will automatically discard the object's lock tag, into the waiting queue state, only if there is another thread called the critical resource of the Notify () or Notifyall () method, it is recommended to use Notifyall (), Before the thread in the wait queue is released, this thread enters the lock pool state.

6) Lock Pool status: Each object has a mutex tag to prevent access to the critical resource resulting in inconsistent data, and data incompleteness. Once a thread has a lock tag for an object, another process wants to access the object and must wait in the lock pool. The system determines which thread gets the lock tag and runs it. Note that the thread from the lock pool state is not immediately back to the running state, but back to the operational state, waiting for the CPU to be dispatched again.

7) Termination Status: A thread is called a terminating state after the end of the run, and only all threads in a process are exited before terminating.

Java Interview 11th Day

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.