Home > Back-end >  Create threads commonly used in two ways
Create threads commonly used in two ways

Time:11-26

Create threads commonly used in two ways:

A:
Create a Thread subclass (DemoThread1) inherit the Thread class, override the run () method, calling start () open a Thread, is not recommended: unavoidable limitations of single inheritance,

Public class TestThread1 extends Thread {
@ Override
Public void the run () {
//run method thread body
for (int i=0; i <200; I++) {
System. The out. Println (" code banging on a straight ");
}
}
Public static void main (String [] args) {
//the main method is the main thread
//create a thread object
TestThread1 TestThread1=new TestThread1 ();

//call the start method open thread
TestThread1. Start ();
for (int i=0; i <100; I++) {
System. Out.println (" hair is getting thinner, terrorist is not you say!! ");
}
}
}

A:
Create a thread subclass (DemoThread2) to realize the Runnable interface, rewrite the run () method, calling start open threads, it is recommended to use: avoid the limitations of single inheritance, convenience of an object is used by multiple threads,

Public class TestThread2 implements Runnable {
@ Override
Public void the run () {
//run method thread body
for (int i=0; i <200; I++) {
System. The out. Println (" code banging on a straight ");
}
}
Public static void main (String [] args) {
//the main method is the main thread
//create the Runnable interface implementation class object
TestThread2 TestThread2=new TestThread2 ();
//create a thread object, open a thread by thread object
New Thread (testThread2.) start ();
for (int i=0; i <100; I++) {
System. Out.println (" hair is getting thinner, terrorist is not you say!! ");
}
}
}

CodePudding user response:

This I know that implements Runnable

CodePudding user response:

This way I think is best when

CodePudding user response:

Better inheritance interface Runnable, Java classes can not inherit multiple consecutive, after inheriting an interface can continue to derived classes

CodePudding user response:

Yes, want to use a few with several

CodePudding user response:

Two ways are the same, is to realize Runnble interface, Thread is punctuated with only the default implementation, this default implementation is introduced into a Runnable need external objects, otherwise you'll have to rewrite the run method,

CodePudding user response:

1. The way a implement Runnable interface
Public class MyRunnable implements Runnable
{
//implementation run method
@ Override
Public void the run ()
{
System. The out. Println (" -- -- -- -- -- run -- -- -- -- -- - ");
}

Public static void main (String [] args)
{
MyRunnable m=new MyRunnable ();
New Thread (m). The start ();
}
}
2. The way two, inherit the Thread class
Public class MyThread extends Thread
{
//rewrite the run method
@ Override
Public void the run ()
{
System. The out. Println (" -- -- -- -- -- run -- -- -- -- -- - ");
}

Public static void main (String [] args)
{
MyThread t=new MyThread ();
t.start();
}
}
  • Related