Java Multithreading thread creation

Source: Internet
Author: User

There are two ways to create thread threads in Java:

1. By inheriting the thread class, overriding the thread's run () method, placing the logic in which the threads run

2. Instantiate the thread class by implementing the Runnable interface

The first way: Inherit the thread class

 PackageCom.yyx.thread;/*** Create threads by inheriting the thread class * Yyx February 4, 2018*/ Public classCreatethreadbyextends { Public Static voidMain (string[] args) {extendthread extendthread=NewExtendthread (); Extendthread.setname ("Thread One");//set the thread nameExtendthread.start ();  for(inti = 1; I <= 100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); }    }}classExtendthreadextendsThread {@Override Public voidrun () { for(inti = 1; I <= 100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); }    }}

Second way: Implement Runnable interface

 PackageCom.yyx.thread;/*** Recommended: Create thread by implementing runnable interface * Yyx February 4, 2018*/ Public classCreatethreadbyinterface { Public Static voidMain (string[] args) {subthread subthread=NewSubthread (); Thread Thread=NewThread (subthread, "thread One")); Thread.Start ();//a thread can only execute once start ()         for(inti = 1;i <= 100;i++) {System.out.println (Thread.CurrentThread (). GetName ()+":" +i); }    }}classSubthreadImplementsRunnable {@Override Public voidrun () { for(inti = 1; I <= 100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ ":" +i); }    }}

The Third Way: create a thread with a return value using Calable and future

 PackageCom.yyx.thread;Importjava.util.concurrent.Callable;ImportJava.util.concurrent.FutureTask;/*** Create a thread with a return value using Calable and future Yyx February 6, 2018*/ Public classCreatethreadbycallableImplementsCallable<integer> {     Public Static voidMain (string[] args) {createthreadbycallable createthreadbycallable=Newcreatethreadbycallable (); Futuretask<Integer> task=NewFuturetask<integer>(createthreadbycallable); NewThread (Task, "Threads with a return value")). Start (); Try{System.out.println (Task.get ()); }Catch(Exception e) {e.printstacktrace (); }} @Override PublicInteger Call ()throwsException {Integer total=0;  for(inti=1;i<=50;i++) { Total+=i; }        returnTotal ; }}

Java Multithreading thread creation

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.