Operating system processes and threads

Source: Internet
Author: User

Processes and threads

First, I would like to explain the concepts and differences between processes and threads. This is a problem that many university teachers cannot clarify.

A process is a running activity of a program with certain independent functions on a dataset,Is an independent unit for system resource allocation and scheduling.. A program is an ordered set of commands. It does not have any running meaning, but is a static entity. The process is different. It is the execution of a program on a certain dataset and a dynamic entity. It is created and runs due to scheduling. It is in the waiting status due to waiting for resources or events and is revoked due to task completion, it reflects all the dynamic processes of a program running on a certain dataset.

  A thread is an entity of a process and the basic unit of CPU scheduling and scheduling.. The thread cannot be executed independently. It must exist in the application and the application provides multiple thread execution control.

The relationship between a thread and a process is as follows: a thread belongs to a process and runs in the process space. The threads produced by the same process share the same memory space, when a process exits, all threads generated by the process are forcibly exited and cleared. A thread can share all resources of a process with other threads of the same process, but it basically does not have system resources, only a bit of information (such as program counters, a set of registers, and stacks) is required during running ).

 

A process is the minimum unit for running the program.

A thread is the smallest unit of CPU scheduling.

Test the thread usage as follows:

View plaincopy to clipboardprint?
Package com. thread;
/**
* Java thread testing
* @ Author Chen xinjie
*
*/
Public class testthread {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs)
{
// Todo auto-generated method stub
Testinnerrunnablethread ();
Testinnerthread ();
New thread (New myrunable (). Start ();
New thread (New myrunable (). Start (); // different objects can call their respective start () Methods

Mythread = new mythread ();
Mythread. Start ();
// Mythread. Start (); multiple start () methods cannot be called at the same time.

// Testthreadstop ();
Testthreadstopwithinterrupt ();


}
/**
* Close the test thread
*/
Private Static void testthreadstop ()
{
System. Out. println ("start! ");
Thread thread = new thread (){
Public void run ()
{
While (true ){
Try {
Thread. Sleep (1000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println ("");
System. Out. println ("disconnected and exited! ");
Break;
}
System. Out. println ("sub-thread executes ing! ");
}
}
};
Thread. Start ();
Try {
Thread. Sleep (5000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();

}

System. Out. println ("stop the subthread! ");
// The main thread waits for 5 seconds and stops the subthread! Exit!
// Thread. Stop ();
Thread. Interrupt ();
System. Out. println ("the main thread ends! ");
}

/**
* When the test thread is closed, isinterrupted is used to stop the thread execution.
*/
Private Static void testthreadstopwithinterrupt ()
{
System. Out. println ("start! ");
Thread thread = new thread (){
Public void run ()
{
While (true &&! Thread. currentthread (). isinterrupted ()){
System. Out. println ("sub-thread executes ing! ");
}
}
};
Thread. Start ();
Try {
Thread. Sleep (5000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();

}

System. Out. println ("stop the subthread! ");
// The main thread waits for 5 seconds and stops the subthread! Exit!
// Thread. Stop ();
Thread. Interrupt ();
System. Out. println ("the main thread ends! ");
}
/**
* Test the anonymous Thread class
*/
Private Static void testinnerrunnablethread ()
{
New thread (New runnable (){
@ Override
Public void run ()
{
// Todo auto-generated method stub
System. Out. println ("innerrunnablethread is running! ");
}
}). Start ();
}
/**
* Test the anonymous Thread class
*/
Private Static void testinnerthread ()
{
New thread (){
Public void run ()
{
System. Out. println ("innerthread is running! ");
}
}. Start ();
}
}
/**
* Test the Thread class inherited from the thread.
* @ Author Chen xinjie
*
*/
Class mythread extends thread {
@ Override
Public void run ()
{
// Todo auto-generated method stub
System. Out. println ("mythread is running! ");
}
}
/**
* Testing the Thread class that implements the runnable interface
* @ Author Chen xinjie
*
*/
Class myrunable implements runnable {
@ Override
Public void run ()
{
// Todo auto-generated method stub
System. Out. println ("myrunable is running! ");
}
}
Package com. thread;
/**
* Java thread testing
* @ Author Chen xinjie
*
*/
Public class testthread {
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs)
{
// Todo auto-generated method stub
Testinnerrunnablethread ();
Testinnerthread ();
New thread (New myrunable (). Start ();
New thread (New myrunable (). Start (); // different objects can call their respective start () Methods

Mythread = new mythread ();
Mythread. Start ();
// Mythread. Start (); multiple start () methods cannot be called at the same time.

// Testthreadstop ();
Testthreadstopwithinterrupt ();


}
/**
* Close the test thread
*/
Private Static void testthreadstop ()
{
System. Out. println ("start! ");
Thread thread = new thread (){
Public void run ()
{
While (true ){
Try {
Thread. Sleep (1000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println ("");
System. Out. println ("disconnected and exited! ");
Break;
}
System. Out. println ("sub-thread executes ing! ");
}
}
};
Thread. Start ();
Try {
Thread. Sleep (5000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();

}

System. Out. println ("stop the subthread! ");
// The main thread waits for 5 seconds and stops the subthread! Exit!
// Thread. Stop ();
Thread. Interrupt ();
System. Out. println ("the main thread ends! ");
}

/**
* When the test thread is closed, isinterrupted is used to stop the thread execution.
*/
Private Static void testthreadstopwithinterrupt ()
{
System. Out. println ("start! ");
Thread thread = new thread (){
Public void run ()
{
While (true &&! Thread. currentthread (). isinterrupted ()){
System. Out. println ("sub-thread executes ing! ");
}
}
};
Thread. Start ();
Try {
Thread. Sleep (5000 );
} Catch (interruptedexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();

}

System. Out. println ("stop the subthread! ");
// The main thread waits for 5 seconds and stops the subthread! Exit!
// Thread. Stop ();
Thread. Interrupt ();
System. Out. println ("the main thread ends! ");
}
/**
* Test the anonymous Thread class
*/
Private Static void testinnerrunnablethread ()
{
New thread (New runnable (){
@ Override
Public void run ()
{
// Todo auto-generated method stub
System. Out. println ("innerrunnablethread is running! ");
}
}). Start ();
}
/**
* Test the anonymous Thread class
*/
Private Static void testinnerthread ()
{
New thread (){
Public void run ()
{
System. Out. println ("innerthread is running! ");
}
}. Start ();
}
}
/**
* Test the Thread class inherited from the thread.
* @ Author Chen xinjie
*
*/
Class mythread extends thread {
@ Override
Public void run ()
{
// Todo auto-generated method stub
System. Out. println ("mythread is running! ");
}
}
/**
* Testing the Thread class that implements the runnable interface
* @ Author Chen xinjie
*
*/
Class myrunable implements runnable {
@ Override
Public void run ()
{
// Todo auto-generated method stub
System. Out. println ("myrunable is running! ");
}
}
 

Two methods for implementing multi-threaded programs:
Inherit from Thread class;
Implement the runnable interface. The only method to implement is void run (). Implement a Thread class by completing the runnable interface.

The START () method of the same thread object cannot be called multiple times. Run () can, however, pass the START () method to the CPU for processing.

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shui1/archive/2010/11/15/6009307.aspx

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.