Java multithreaded Programming (i)

Source: Internet
Author: User

One, thread and process:

1. Threads: Control flow in a separate sequence in a program

The thread itself relies on the program to run

Threads are sequential control flows in a program and can only use resources and environments that are assigned to programs

2. Process: procedures in progress

A process can contain one or more threads

A process must contain at least one thread

3. Single Thread:

There must be at least one main thread in the program, in fact the main method is a main thread.

4. Multithreading:

Multithreading is the task of running multiple tasks in a program

The purpose of multithreading is to better use CPU resources

Second, the implementation of the thread

1. In Java, there are two implementations of threads: inheriting the thread class, implementing the Runable interface

2.Thread class:

The thread class is defined in the Java.lang package, and inheriting the thread class must override the run () method

define the format:

Class ClassName extends thread{run () {}; }

3.Runable interface

Specific code actions:

first implementation: Inheriting The Tread class

Package Com.yeqc.thread;public class MyThread extends Thread{private string name;public MyThread (string name) {THIS.name = name;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println (name+ ":" +i);} Super.run ();}} Package Com.yeqc.thread;public class ThreadDemo01 {public static void main (string[] args) {MyThread T1 = new MyThread ("A") ; MyThread t2 = new MyThread ("B");//t1.run ();//t2.run ();//thread startup is through Start () T1.start (); T2.start ();}}

Operation Result:

B:0a:0b:1a:1b:2a:2b:3b:4b:5b:6a:3a:4a:5b:7a:6b:8a:7b:9a:8a:9

Second implementation: Implementing the Runable Interface

Package Com.yeqc.thread;public class Myrunable implements Runnable{private string Name;public myrunable (string name) { THIS.name = name;} @Overridepublic void Run () {for (int i = 0; i < i++) {System.out.println (name+ ":" +i);}} Package Com.yeqc.thread;public class ThreadDemo01 {public static void main (string[] args) {myrunable r1 = new Myrunable ("A "); myrunable r2 = new Myrunable ("B"); thread T1 = new Thread (r1); Thread t2 = new Thread (r2); T1.start (); T2.start ();}}

Operation Result:

B:0a:0b:1a:1b:2b:3a:2b:4b:5a:3b:6a:4b:7a:5b:8a:6b:9a:7a:8a:9


Java multithreaded Programming (i)

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.