Java multithreading and Multithreading

Source: Internet
Author: User

Java multithreading and Multithreading

What is multithreading? Let's not talk about these things. Let's look at the example.

I. Multithreading 1. inherit the Thread class

Package com. ztz. myThread; public class MyThread extends Thread {@ Overridepublic void run () {System. out. println ("Inheriting Thread");} public static void main (String [] args) throws Exception {MyThread t = new MyThread (); t. start ();}}

2. Implement the Runnable interface

Package com. ztz. myThread; public class MyRunnable implements Runnable {@ Overridepublic void run () {System. out. println ("Implementing the Runnable interface");} public static void main (String [] args) throws Exception {MyRunnable runnable = new MyRunnable (); Thread thread Thread = new Thread (runnable ); thread. start ();}}

PS: the start () method is used to start a thread, not the run () method. The method of inheriting the Thread class has limitations, because java is a single inheritance. To change this restriction, we can implement the Runnable interface to implement multithreading.


Ii. currentThread () methodThe currentThread () method returns the information of the thread that is calling the code snippet.
package com.ztz.myThread;public class MyThread{public static void main(String[] args)throws Exception {System.out.println(Thread.currentThread().getName());}}
Run this method and the console outputs: main
Note: The main method is called by the main thread. Thread. currentThread (). getName () is used to obtain the Thread name.
Package com. ztz. myThread; public class MyThread extends Thread {public MyThread () {System. out. println ("constructor:" + Thread. currentThread (). getName () ;}@ Overridepublic void run () {System. out. println ("run:" + Thread. currentThread (). getName ();} public static void main (String [] args) throws Exception {MyThread thread = new MyThread (); thread. start ();}}
Run this method. Console output: constructor: main
Run: Thread-0

In fact, main is the main Thread. Thread-0 is a sub-Thread. If a Thread is started, it is Thread-1.

Iii. sleep () methodThe sleep () method is used to sleep the currently executing thread (pause execution) within a specified millisecond. The executing thread refers to the thread returned by this. currentThread.
package com.ztz.myThread;public class MyThread extends Thread{@Overridepublic void run() {try{System.out.println("run start:"+System.currentTimeMillis());Thread.sleep(2000);System.out.println("run end:"+System.currentTimeMillis());}catch(Exception e){}}public static void main(String[] args)throws Exception {MyThread thread=new MyThread();System.out.println("thread start:"+System.currentTimeMillis());thread.start();System.out.println("thread end:"+System.currentTimeMillis());}}

Output result: thread start: 1437233204468
Thread end: 1437233204468
Run start: 1437233204468
Run end: 1437233206468
Iv. getId () methodThe getId () method is used to obtain the unique identifier of a thread.
package com.ztz.myThread;public class MyThread{public static void main(String[] args)throws Exception {Thread currentThread = Thread.currentThread();System.out.println(currentThread.getName()+"--->"+currentThread.getId());}}

Output result: main ---> 1



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.