[Java] - thread

Concurrent and Parallel

Concurrency:

Refers to two or more events occur in the same period a

parallel:

Refers to two or more events occur at the same time (concurrently)

process

Refers to an application running in memory, each process has a separate memory space, an application can run multiple processes at the same time, once the process of execution of the program is, is the basic unit of the system to run the program. The system is running a program that is created from a process, run to the demise of the process.

Thread

Is a process of execution units, is responsible for the current process in the execution of the program ,, a process in which at least one thread. A process can have multiple threads, the application can also be called multi-threaded programs.
In short: there is at least one process after running a program, a process can contain multiple threads.

Thread scheduling:

Time-sharing scheduling: the right to use all the threads take turns using the CPU, the average time each thread CPU-allocation.

Preemptive scheduling: giving priority to high-priority threads CPU, if the priority of the thread of the same, then will randomly select one (thread randomness), Java for use preemptive scheduling.

The main thread:

Thread executing the main (main) method. JVM execute the main method main method will enter into JVM memory stack will find the operating system to open up a main method of execution path leading to the cpu cpu main method can be performed through this path and this path has a name, called main (main) thread.

Multithreading:

Create multithreaded programs first way: Thread subclass

. java lang Thread class: Thread is a description of the class, we want to achieve a multi-threaded program, it must extend the Thread class.
the Java word belongs preemptive scheduling process, the high-priority thread, that thread takes precedence; the same priority randomly selects an execution

package com.itheima.demo.Thread;

// 1.创建一个Thread类的子类
public class MyThread extends Thread{
    //2.在Thread类的子类中重写Thread类中的run方法,设置线程任务(开启线程要做什么?)
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("run:"+ i );
        }
    }

}


public class Demo01Thread{
    public static void main(String[] args) {
        //3.创建Thread类的子类对象
        MyThread mt = new MyThread();
        //4.调用Thread类中的方法start方法,开启新的线程,执行run方法
        mt. start();
    
        for ( int i = 0; i < 20; i++) {
            System.out.println("main:"+ i );
        }
    }
}

The results of program execution: randomness Print

对于cpu而言,就有了两条执行的路径,cpu就有了选择的权利,cpu喜欢谁,就会执行那条路径,我们控制不了cpu,所有就有了程序的随机打印结果。
两个线程一个main线程,一个新线程起抢夺cpu的执行权(执行时间),谁抢到了谁执行对应的代码。

创建多线程程序的第二种的方式:实现Runnable接口

java. Lang. Runnable
        Runnable接口应该由那些打算通过某一线程执行其实例的类来实现。类必须定义一个称为run的无参数方法。
java. lang. Thread类的构造方法
        Thread(Runnable target) 分配新的Thread 对象。
        Thread(Runnable target, String name) 分配新的Thread 对象。
 

//1.创建一个Runnable接口的实现类
public class RunnableImpl implements Runnable{
    //2.在实现类中重写Runnable接口的run方法,设置线程任务
    @Override
    public void run() {
        for(int i = 0; i<20; i++) {
            System.out.println(Thread.currentThread().getName()+"-->"+i);
        }
    }
}


public class Demoe1Runnable {
    public static void main(String[] args) {
        //3.创建一个Runnable接口的实现类对象
        RunnableImpl run = new RunnableImpl();
        //4.创建Thread类对象,构造方法中传递Runnable接口的实现类对象
        Thread t = new Thread(run);
        //5.调用Thread类中的start方法,开启新的线程执行run方法
        t.start();
        for(int i = 0; i < 20; i++) {
            System.out. println(Thread . current Thread(). getName()+*-->"+i);
        }
    }
}

程序执行结果:

 

发布了215 篇原创文章 · 获赞 37 · 访问量 8万+

Guess you like

Origin blog.csdn.net/MirabelleZWH/article/details/101828085