More than 11 threads

1. Introduction Multithreading

Process: The process of correction program is running. The exact, when a program into the memory to run, that becomes a process, the process is in the process of running the program, and with a certain separate functions.

Thread: A thread is a process execution unit, responsible for the implementation of the program in the current process, a process in which at least one thread. A process can have multiple threads, the application can also be called multi-threaded programs.

2. Create a thread

java provides two ways to create threads:

  • One way is to declare the class as a subclass of Thread. The method of the Thread class run subclasses should override. Create objects, open thread. run method is equivalent to the main method other threads.
  • Another method is to declare a class that implements the Runnable interface. This class then implements the run method. Then create a subclass of Runnable objects, passed to the constructor of a thread, open thread.

2.1 by extending the Thread class to create a thread

package cn.jxufe.java.chapter10.demo01;

/*
 * Create and start a thread
 * Create a Thread subclass object
 * Subclass object method calls start ()
 * Let threaded program execution, JVM calling thread run
 */
public class Test01Thread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SubThread st = new SubThread();
        st.start();
        for (int i = 0; i < 50; i++) {
            System.out.println("main..." + i);
        }
    }

}

/*
 * Define a subclass inherits Thread 
 * Override method run 
 */
class SubThread extends Thread {
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println("sub..." + i);
        }
    }
}

Thoughts: thread object and call the run method calls the difference between the start method?

Thread object calls the run method is not open thread. Only the object method call. Open thread object start calling thread and let jvm calls the run method of execution in the open thread.

2.2 Get Thread name

package cn.jxufe.java.chapter10.demo01;

/*
 * Each thread has its own name
 * Run method main thread, the name is "main"
 * Thread the other new key also has a name, the default "Thread-0", "Thread-1"
 *  
 * JVM open the main thread running method main, the main thread is the thread, the thread is inevitable that
 * Thread class object
 * Thread class, static methods
 * Static Thread currentThread () returns the thread object being executed
 */
public class Test02Thread {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        NameThread nt = new NameThread();
        nt.setName ( "Cai" );
        nt.start();

        /*Thread t =Thread.currentThread();
        System.out.println(t.getName());*/
        System.out.println(Thread.currentThread().getName());
    }

}

class NameThread extends Thread {
    @Override
    public void run() {
        System.out.println(getName());
    }
}

2.3sleep method

 

package cn.jxufe.java.chapter10.demo01;

public class Test03Sleep {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        /*for(int i = 0 ; i < 5 ;i++){
        Thread.sleep(50);
        System.out.println(i);
        }*/

        new SleepThread().start();
    }

}

class SleepThread extends Thread {
    public void run() {
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(1000);
            } catch (Exception ex) {

            }
            System.out.println(i);
        }
    }
}

2.4 Example

package cn.jxufe.java.chapter10.demo01;

public  class Test04ThreadDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ThreadExample te1 = new ThreadExample("线程1");
        ThreadExample te2 = new ThreadExample("线程2");
        ThreadExample te3 = new ThreadExample("线程3");
        te1.start();
        te2.start();
        te3.start();
    }

}
class ThreadExample extends Thread{
    public ThreadExample(String s) {
        // TODO Auto-generated constructor stub
        setName(s);
    }
    
    public void run() {
        for (int i = 0; i < 5; i++) {
            System.out.println ( "first" + i + "invocation" + getName ());
        }
    }
}

3. Create a thread by implementing Runnable interface

Another way to create a thread is to declare a class that implements the Runnable interface. This class then implements the run method. Then create a subclass of Runnable objects, passed to the constructor of a thread, open thread.

Why implement Runnable, Runable hell is it? Continue Search API.

View Runnable interface documentation: Runnable interface is used to specify the tasks to be performed for each thread. No abstract method contains a run of parameters, which need to be rewritten by the implementation of that interface.

 

Guess you like

Origin www.cnblogs.com/xinmomoyan/p/11008148.html