Java Learning notes----threads

Source: Internet
Author: User

Continue to learn-----------------------------------------------------------------------

In the Java thread refers to two things

1) An example of the Java.lang.Thread class

2) Execution of threads

A thread class instance is just an object that lives and dies on the heap. The Main method is a main thread


Java Thread creation and startup:

1) Extended Java.lang.Thread class

2) Implement Java.lang.Runnable interface

Instantiating Threads:

1) If you are extending the thread, simply use new to

2) If you are a class that implements the Runnable interface, use the thread's construction method

Thread (Runnable target) thread (Runnable target, String name) thread (threadgroup Group, Runnable target) thread ( Threadgroup Group, Runnable Target, string name) Thread (threadgroup Group, Runnable Target, string name, long stackSize)

To start a thread:

Call the Start method

Start a new execution thread

Running the Run method


Instance code:

Package Com.ehealth.thread;public class Runnabledemo implements Runnable{private String name;public Runnabledemo ( String name) {this.name=name;} @Overridepublic void Run () {for (int. i=0;i<5;i++) {for (int k=0;k<5;k++) System.out.println (name+ ":" +i);}}}
Package Com.ehealth.thread;public class Testthread extends Thread{public testthread (String name) {super (name);} public void Run () {for (int. i=0;i<5;i++) {for (int k=0;k<5;k++) {System.out.println (This.getname () + ":" +i);}}} public static void Main (string[] args) {Thread t1=new testthread ("JOHNW"); Thread t2=new testthread ("wee"); T1.start (); T2.start ();}}

Own understanding of:

If the Runnable interface is implemented, the Run method must be implemented. When you start a thread, you must first new itself, the thread object in new, and pass yourself as a parameter to thread.

If you inherit the thread class, rewrite the Run method again, and then new will be able to start

The Run method contains the execution logic of the thread that you want to implement.

It feels like too many threads. Here is the first excerpt and summarize the basis of it. Finally, you can learn to combine sockets with threads and IO streams.

This article from "ehealth" blog, declined reprint!

Java Learning notes----threads

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.