Java multi-threading (a): threads and processes

1. threads and processes

1.1 Process

Process is the concept of the operating system, we run a TIM.exe is a process.

Process (Process) is a computer program 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. In the computer architecture for the early design process, the basic process is the execution of the program entity; in contemporary computer architecture design for the thread, the thread is the process vessel. Program is instruction, organization of data and its description of the process is a solid program

1.2 thread

A thread is attached to the process exist, each thread must have a parent process;
thread has its own stack, program counter, and local variables, system resources, thread and other threads share the process;
the process can not be shared memory, and can be between threads easily shared memory

2. The significance of multi-threaded

2.1 of multi-core processors for maximum performance

As a quad-core processor to run single-threaded task, a core can only run one thread, then the three core performance will be wasted. Another example is the 32-core CPU server running a single-threaded task, 31 core in the "lazy", largely a waste of server performance;
such as blogger computer CPU is i7 6700HQ, quad-core eight threads. The CPU uses 超线程技术. Simply put, a single-core processor, dual-core to simulate the environment, but it is not able to double the performance of the processor upgrade, because the core is always only one entity, while the performance of about twenty percent to thirty growth.
We can be understood as castrated version of the eight-core processor, can not afford to buy an eight-core, eight thread processors also can not afford it?

2.2 play single-core processors for maximum performance

If the process is single-threaded, it is waiting for an I / O operation is complete, then the processor is idle; if the process is multi-threaded, a thread is waiting for some time I / O operation is complete, another thread It can be performed.
The case of single-core processor executes multiple threads

1.单核CPU同一时间,CPU只能处理1个线程,只有1个线程在执行
2.多线程同时执行:是CPU快速的在多个线程之间的切换
3.CPU调度线程的时间足够快,使我们产生错觉,多线程“同时”执行
4.如果线程数非常多,CPU会在n个线程之间切换,消耗大量的CPU资源,每个线程被调度的次数会降低,线程的执行效率降低

3.创建线程

3.1 继承Thread

继承Thread,重写run方法。

public class MyThread00 extends Thread{
    public void run()
    {
        for (int i = 0; i < 50; i++)
        {
            System.out.println(Thread.currentThread().getName() + "在运行!");
        }
    }


    public static void main(String[] args)
    {
        MyThread00 mt0 = new MyThread00();
        //启动线程
        mt0.start();

        //main线程
        for (int i = 0; i < 50; i++)
        {
            System.out.println(Thread.currentThread().getName() + "在运行!");
        }
    }
}

start方法:
调用start方法Java虚拟机会启动run方法;
一个线程不能多次调用start方法;
死去的线程不能被重启;
执行结果如下:

main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
main在运行!
Thread-0在运行!
Thread-0在运行!
Thread-0在运行!
Thread-0在运行!
Thread-0在运行!
Thread-0在运行!
Thread-0在运行!
Thread-0在运行!
......

线程交替执行,不会按照固定顺序执行,每次执行的结果都不一致。

3.2 实现Runnable

实现Runnable接口,重写run方法;有利于代码解耦。

public class MyThread01 implements Runnable
{
    public void run()
    {
        for (int i = 0; i < 50; i++)
        {
            System.out.println(Thread.currentThread().getName() + "在运行!");
        }
    }

    public static void main(String[] args)
    {
        MyThread01 mt0 = new MyThread01();
        Thread t = new Thread(mt0);
        //启动线程
        t.start();
        //main线程
        for (int i = 0; i < 50; i++)
        {
            System.out.println(Thread.currentThread().getName() + "在运行!");
        }
    }
}

执行结果类似于3.1

3.3 实现Callable

实现Callable创建线程,重写call方法,该方法可以返回值和抛出异常

public class MyThread03 implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("计算处理中...");
        Thread.sleep(3000);
        return 1;
    }

    public static void main(String[] args) {
        //构建任务
        MyThread03 t = new MyThread03();
        //执行Callable方式,需要FutureTask实现类的支持,用于接收运算结果
        FutureTask<Integer> task = new FutureTask<Integer>(t);
        //启动线程
        new Thread(task).start();
        //获取结果
        try {
            Integer integer = task.get(5000,TimeUnit.MILLISECONDS);
            System.out.println("线程执行结果:"+integer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

执行结果如下

计算处理中...
线程执行结果:1

我们获取到了call方法的返回值,继承Thread和实现Runnable方式创建线程无法获得返回值

Guess you like

Origin www.cnblogs.com/Java-Starter/p/11057987.html