To create a thread that inherits thread
/ * * Create thread Mode one: inherit from thread * Step: * 1. Custom class, inherited from Thread class * 2. Override the Run () method * 3. Create an instance of the class, that is, create a thread * 4. Call the thread's start () method to: Start the thread, call the Run () method * * * thread's Run method, used to store the code to be executed by the thread, must override this method * This method is called automatically when the thread is started * / class Demo extends Thread{@Override Public voidRun () { for(intI=1; i<= -; i++) {System.out.println ("*****run*******"+i); } }} Public class Test01 { Public Static voidMain (string[] args) {Demo demo=NewDemo ();//Create a threadDemo.start ();//Start the thread and execute the thread's Run method //demo.run (); Just the method that called the object, although the thread was created but did not start for(intI=1; i<= -; i++) {System.out.println ("*****hello world*******"+i); } }}
Create Two threads
/ * * Create two threads * * Thread default name * Custom thread: thread-number, starting from 0 * Main thread: main * * View Thread name * GETNAME () * Thread.CurrentThread () get current thread instance * * Set Thread Name * by construction method, * */ class MyThread extends Thread { PublicMyThread (String name) {Super(name); } @Override Public voidRun () {System.out.println (thread.currentthread () = = This); for(inti =1; I <= -; i++) {//system.out.println (GetName () + "*****run*******" + i);System.out.println (Thread.CurrentThread (). GetName () +"*****run*******"+ i); } }} Public class Test02 { Public Static voidMain (string[] args) {//Create two threads and startMyThread T1 =NewMyThread ("Thread One"); MyThread t2 =NewMyThread ("Thread Two"); T1.setname ("First"); T1.start (); T2.start (); Thread Thread=thread.currentthread ();//Get current thread instance for(inti =1; I <= -; i++) {System.out.println (Thread.getname () +"*****hello world*******"+ i); } }}
Java Learning Note (37)-Creating threads that inherit thread