And three ways to use Java to create a thread basis _ threads

 

 

  Thread: A thread is the smallest unit of an operating system capable of operation scheduling. It is included in the process, the actual operation of the unit in the process . A thread refers to a single control flow of a process sequence, a process can be complicated by a plurality of threads, each thread in parallel to perform different tasks.

  Process: The process is computer programs run on a set of data on the activities of the system is the basic unit of resource allocation and scheduling, is the underlying operating system architecture.

  A simple explanation of processes and threads (Ruan Yifeng)  Portal

 

 

  Java to create a thread, Method One: Thread class inheritance

  • Inheritance Thread
  • Override the run method
  • Create objects and call the start method to run

  (If you call the run method directly equivalent to calling an ordinary method, and do not start calling thread to run)

 

public class MyThread extends Thread{
    @Override
    public void run() {   
        for(int i=0;i<1000;i++) {
            System.out.println("MyThread "+i);
        }   
    }
}

 

package com.Gary1;

public class MyThread extends Thread{

    @Override
    public void run() {
        
        for(int i=0;i<1000;i++) {
            System.out.println("MyThread "+i);
        }
        
    }


}
MyThread.java

 

  The main method to start this thread, start the thread () method called directly, instead of calling run () method

  Run the calling thread () method, find the thread did not execute concurrently, just a simple method call

  

 

Package com.Gary1; 

public  class the CreateThread { 

    public  static  void main (String [] args) { 
        
        the MyThread thread = new new the MyThread (); 
        
        Thread.run ();         // error, thread.run () refers to the direct calling thread inside thread run method
         // Thread.start ();         // start threads, run method is automatically called 
        
        for ( int I = 0; I <1000; I ++ ) { 
            System.err.println ( "MainThread" + I); 
        } 
        
    } 
    
}
CreateThread.java

 

  Start calling thread () method to start a thread, you can see the program in the main thread and the thread is executed concurrently MyThread

  

 

package com.Gary1;

public class CreateThread {

    public static void main(String[] args) {
        
        MyThread thread = new MyThread();
        
        //thread.run();        //错误的,thread.run()指直接调用thread线程里边的run方法
        thread.start();        //启动线程,run方法自动调用
        
        for(int i=0;i<1000;i++) {
            System.err.println("MainThread "+i);
        }
        
    }
    
}
CreateThread.java

 

 

  a)获取(当前)线程名字,设置名字

  Thread mainThread = Thread.currentThread();

 

    //设置线程名字
    mainThread.setName("Gary");
    //获得线程名字
    System.out.println(mainThread.getName());

 

  

 

  b)线程调度规则(线程调度会整体上是遵守下面的规则,但是从单个上来看是随机的)

    分时调度(平均分配):10个线程执行100毫秒CPU,每个线程平均分配10毫秒的CPU

    抢占式调度(按照优先级)(Java使用的调度规则):优先级高的,有更高的几率被CPU所执行

    获得线程优先级:thread.getPriority()

    MyThread thread = new MyThread();
    //获得线程优先级
    System.out.println(thread.getPriority());

 

  

 

 

  c)设置优先级为1,mainThread.setPriority()

    mainThread.setPriority(1);

 

   d)线程休眠,让当前线程休眠(单位毫秒)

    Thread.sleep();

 

   e)join 把某个线程加入到当前线程中(Main()方法中为主线程,)

    t1.join();

 

   f)设置守护线程setDaemon(true)

   如果程序中只剩下守护线程在运行,那么程序会停止

    t2.setDaemon(true);    

 

  g)线程中断

    stop()启用(被弃用)

    interrupt() 让线程自己抛出异常,让线程自己可以处理被终止的时候做一些事情

    t1.stop();
    t1.interrupt();

 

 

  Java中创建一个线程,方法二:实现Runnable接口

    a)实现Runnable接口

    b)实现run方法

    c)创建当前类的对象和Thread的对象,并使用thread对象启动线程

 

    a)获取当前线程的名字

        Thread.currentThread().getName()

        设置名字通过Thread对象

    b)构造方法

        Thread t = new Thread(Runnable target);

        Thread t = new Thread(Runnable target,String name);

package com.Gary1;

public class MyThread2 implements Runnable{

    @Override
    public void run() {
        
        for(int i=0;i<100;i++) {
            //获得当前线程
            Thread t = Thread.currentThread();
            
            System.out.println(t.getName()+i);
        }
        
    }
}
MyThread2.java

 

package com.Gary1;

public class CreateThread2 {

    public static void main(String[] args) {
        
        //创建当前类的对象和Thread的对象
        MyThread2 t = new MyThread2();
        //并使用thread对象启动线程
        Thread t1 = new Thread(t);
        t1.start();
        
        Thread t2 = new Thread(t,"线程2");
        t2.start();//调用了同一个Thread2的run()方法
        
    }
    
}
CreateThread2.java

 

  方式二的好处:

    可以避免单继承带来的局限性(实现了接口后,可以继承一个别的类)

    可以很好的处理两个线程共享一个资源的情况

 

  Java中创建一个线程,方法三:使用匿名内部类

    a)new Runnable(){}

    b)new Thread(){}

  

 

package com.Gary1;

public class CreateThread3 {

    public static void main(String[] args) {
        //匿名内部类
        //适用于线程只在这里实现以此
        Runnable r = new Runnable() {

            @Override
            public void run() {
                for(int i=0;i<100;i++) {
                    System.out.println(Thread.currentThread().getName()+":"+i);
                }
            }

        };
        
        //    Thread t = new Thread(r,"匿名内部类线程");
        //    t.start();
        
        new Thread(r,"匿名内部类线程").start();
        
    }
}
CreateThread3.java

 

 

Guess you like

Origin www.cnblogs.com/1138720556Gary/p/11941471.html