Getting Started with threads (i)

Source: Internet
Author: User
Tags stub thread class
what is a thread. First: The concept of programs, processes, threads1. Program is a set of instructions that are written in a language to accomplish a specific task. Refers to a static code, static object. 2. Processes (process) are an execution of a program, or a program that is running. Dynamic process: It has its own process of generation, existence and extinction. Such as: Running in the QQ, the running of the MP3 player. The program is static and the process is dynamic

3. Threads (thread), a process that can be further refined into threads, is an execution path within a program. If a program can execute multiple threads at the same time, it supports multithreading. Understanding the relationship between processes and threads is the basis of learning threads. A process is an instance of an application that is running in the system, which includes a complete process of code loading, execution, and execution, as well as the process itself from generation to development to extinction. A multitasking operating system can run multiple programs at the same time, meaning that multiple processes exist and run simultaneously. A thread is a smaller execution unit than a process, and is further divided on a process basis. Multithreading means that a process can produce multiple simultaneous threads that run simultaneously during execution. Multi-threaded mechanism can use resources rationally and improve program running efficiency. A process contains at least one thread, which automatically generates a thread when the program runs, such as the main function main in Java, which runs on a thread. The current program is single-threaded, except that the main thread no longer creates a type. The principle of multithreading and its relationship with the process are as follows: second: When multiple threads are required. a program needs to perform two or more tasks at the same time. Programs need to implement some tasks that need to wait, such as user input, file read and write operations, network operations, search and so on. When you need some background-run programs. Thread Implementation

There are two kinds of multithreading in Java: Inheriting thred classes and implementing runnable interfaces. One: Inherit the thread class

Java uses the Java.lang.Thread class to control the thread, and the class that inherits the thread can be called a multithreaded operation class. Note that the run () method in the thread class is overridden in a subclass of an inheritance thread class, which is the body of the threads.

To inherit the thread class calling the threading process:

1. Inherit Thread class

2. Implement the Run () method

3. Call the Start () method, call the Run () method, and start the thread execution thread body.

Package com.process;


/**
 * Thread class analysis
 * @author Administrator
 * * *
 * * * * * The first step inherits the thread class
/public class Threaddemo extends Thread {
	private String name;

	Public Threaddemo (String name) {
		this.name = name;
	}
/**
 * Step two: Rewrite the Run () method in the thread class *
 *
	@Override public
	void Run () {
		//TODO auto-generated methods Stub
		for (int i=1;i<=5;i++) {
			System.out.println (name+ "Run, i=" +i);
		}
	
	public static void Main (string[] args) {
		/
		 * * This only invokes the run () method, which does not enable threading. It is obvious that the result of the program operation must be the
		 * thread A's Run method, and then go to Thread B's Run method
		 /
		new Threaddemo ("Thread A"). Run ();
		New Threaddemo ("Thread B"). Run ();


This is an error case. I have to keep these things in mind for the rookie. You can see that the running result of the program is to execute "thread a" first, then execute "thread B", thus the popularity problem arises: multithreading is not executed concurrently. , and why it is executed sequentially. Multithreading at the same time exist, running at the same time the characteristics of where.

Answer: Very simple. I did not start the thread, just called the Run () method of two objects, first A, execution finished, in the tuning B.

The proper threading method is as follows: Simply call the Run () method and change to the start () method, which is the correct way to start the thread.

Package com.process;


/**
 * Thread class analysis
 * @author Administrator
 * * *
 * * * * * The first step inherits the thread class
/public class Threaddemo extends Thread {
	private String name;

	Public Threaddemo (String name) {
		this.name = name;
	}
/**
 * Step two: Rewrite the Run () method in the thread class *
 *
	@Override public
	void Run () {
		//TODO auto-generated methods Stub
		for (int i=1;i<=5;i++) {
			System.out.println (name+ "Run, i=" +i);
		}
	
	public static void Main (string[] args) {
		/**
		 * and how the thread is properly enabled what should be the third step?
		 * Step three: The correct way to enable threading is to invoke the start () method that is inherited from the thread class.
		 */
		new Threaddemo ("Thread C"). Start ();
		New Threaddemo ("Thread D"). Start ();
	}

Obviously, you can see the staggered operation of the resources, namely: which object to obtain CPU resources first can run.

The above two printing results show that two threads are staggered, that is, which thread object preemptive CPU resources can be selected to run, the thread starts with the start () method calls the run () method, execution body.

A piece of code that needs to run in a new thread must be placed in the class's run () method, while a thread class object allows only one call to the start () method, and multiple invocations produce an exception.

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.