Create multi-threaded & Thread class common method inherited way

One way to create multiple threads: class inheritance java.lang.Thread

Note: 1 can only be executed once a thread start ()

           2. run can not be achieved through the Thread class object () to start a thread

           3. Increase plus a thread, you need to create a new object is a thread

 

Thread class common method:

            1.start (): Start a thread and execute the corresponding run () method

             2.run (): Code sub-thread to be executed into the run () method

             3.currentThread (): static, to get the current thread

             4.getName (): Gets the name of this thread

             5.setName (): Set the name of this thread

             6.yield (): This method is called a thread releases the current execution of the CPU

             7.join (): A calling thread B in the thread join () method, said: When performing this method,

                            A thread stops execution until the thread B is finished,

                            A thread then join again the code (after) performing

             8.isAlive (): determine whether the current thread is still alive

             9.sleep (long m): Let's show the current thread to sleep m ms

            10. The thread communication: wait () notify () notifyAll ()

             Set the thread priority:

                                 getPriority (): Returns the thread priority value

                                 setPriority (int newPriority): change the priority of the thread 

 

Object anonymous class inheritance Thread class

Testt

package com.aff.thread;

public class TestTh {
    public static void main(String[] args) {
        //继承Thread类的匿名类的对象
        new Thread() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 == 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();

        new Thread() {
            public void run() {
                for (int i = 0; i < 100; i++) {
                    if (i % 2 != 0) {
                        System.out.println(Thread.currentThread().getName() + ":" + i);
                    }
                }
            }
        }.start();
    }
}

 

Guess you like

Origin www.cnblogs.com/afangfang/p/12612160.html