Java basics-threads

Source: Internet
Author: User

Conceptual process-a process is a running activity of a program with certain independent functions about a dataset. A process is an independent unit for the system to allocate and schedule resources. thread-thread is a single sequential control flow in the program. Difference 1) a process is the unit of resource allocation by the operating system. A thread is the unit of execution by the operating system. 2) A thread is an entity of a process and the basic unit of CPU scheduling and dispatching. It is a smaller basic unit that can run independently than a process. the thread itself basically does not have system resources, and only has a few resources (such as program counters, a set of registers and stacks) that are essential for running ), however, it can share all resources of a process with other threads of the same process. one thread can create and cancel another thread, and multiple threads in the same process can be concurrently executed. 3) address space and other resources (such as opening files): processes are independent of each other and the threads of the same process are shared. Threads in a process are invisible to other processes. 4) Communication: Interprocess Communication IPC. interthreads can directly read and write process data segments (such as global variables) for communication. Process Synchronization and mutex are required to ensure data consistency. 5) scheduling and switching: thread context switching is much faster than process context switching. 6) in a multi-threaded OS, a process is not an executable entity. Multithreading is called multithreading. In a multi-threaded system, a process contains at least one thread. The operating system creates a process to solve a problem, multiple Threads can be used when the process can be divided into multiple processes and the execution of these processes is in ascending order or the results do not affect each other. Otherwise, they cannot. Analysis: Process 1 is created. Process 1 requires three processes: Process 1, process 2, and process 3. Because the three processes must be executed in the order of 1-2-3, that is, they affect each other, therefore, process 1 requires only one thread to complete the task (a process contains at least one thread ). Process 2 also contains three processes. Different from process 1, the execution sequence and effect of process 2 and process 3 do not affect each other, that is, process 2 and process 3 are relatively independent, therefore, you can use two threads to complete the task. Thread 1 executes process 1 first. After process 1 is completed, thread 2 is created. At this time, thread 1 can continue to complete process 2 and thread 2 to Execute Process 3. After thread 1 and thread 2 exit, the entire process task is completed. Two ways to create a thread: 1. Implement the Runnable interface to implement the run Method

Public class Thread1 implements Runnable {private Thread t; @ Override public void run () {// TODO Auto-generated method stub for (int I = 0; I <= 10; I ++) {System. out. println (I); // print 1-10 try {t. sleep (1000); // print once every 1 second} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public void start () {t = new Thread (this); t. start (); // start a new thread} public static void main (String args []) {Thread1 th = new Thread1 (); th. start (); for (int I = 0; I <10; I ++) {System. out. println ("hello word"); // print hello word try {new Thread (). sleep (500); // print every half second} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}

 

Ii. inherit the Thread class and override the run Method
Public class Thread2 extends Thread {@ Override public void run () {// TODO Auto-generated method stub super. run (); for (int I = 0; I <10; I ++) {System. out. println (I); // print 0-10 try {new Thread (). sleep (500); // print every half second} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} public static void main (String args []) {Thread2 the = new Thread2 ();. start (); // start thread for (int I = 0; I <10; I ++) {System. out. println ("hello word"); // print "hello word" try {new Thread (). sleep (1000); // print once every second} catch (InterruptedException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}}

 

The summary process is static, while the thread is dynamic. The process is like a family and a social cell. The thread is like a family member. Note that family members can share household resources such as chopsticks, bowls, TVs, sofa. But it is generally not shared among families because they use their own. In multi-threaded programming, the use of threads can put tasks in programs that take up a long time into the background for processing, so that the program runs faster and saves time. By improving the resource usage efficiency, the system efficiency is improved. Although multi-threaded programming has great advantages, if there are a large number of threads, it will affect the performance, because the operating system needs to switch between them. More threads require more memory space. Threads may bring more bugs to the program, so be careful when using them. The impact of thread suspension on program running must be considered. Generally, block model data is shared among multiple threads, so it is necessary to prevent thread deadlocks and thread security problems caused by synchronous thread execution.

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.