Home  >  Article  >  Java  >  What is Java multithreading

What is Java multithreading

(*-*)浩
(*-*)浩Original
2019-05-05 11:52:3414510browse

This article will introduce multi-threading and its examples.

What is Java multithreading

#Java programs are all run in the Java Virtual Machine (JVM). Inside the JVM, multitasking of the program is implemented through threads. Every time a java application is started with the java command, a JVM process will be started. In the same JVM process, there is only one process, which is itself. In this JVM environment, all program code runs in threads.

Generally common Java applications are single-threaded. For example, when you use the java command to run the simplest HelloWorld Java application, a JVM process is started. process, the JVM finds the entry point main() of the program, and then runs the main() method, thus generating a thread, which is called the main thread. When the main method ends, the main thread is completed. The JVM process also exits immediately.

For multiple threads in a process, multiple threads share the memory block of the process. When a new thread is generated, the operating system does not allocate new memory, but allows the new thread to share the original memory block. Some processes block memory. Therefore, communication between threads is easy and fast. Because different processes are in different memory blocks, communication between processes is relatively difficult.

Recommended course: Java tutorial

Process refers to an application running in memory. Each process has its own independent piece of memory. Space, multiple threads can be started in one process. For example, in Windows systems, a running exe is a process.

Thread refers to an execution process in a process. A process can run multiple threads. For example, the java.exe process can run many threads. Threads always enter a process, and multiple threads in a process share the process's memory.

Multi-threading refers to the fact that more than one thread is generated when this program (a process) is running

Instance

Thread 1: Next, use the method of implementing Runnable to implement multi-threading:.

public class TestRunnable implements Runnable {

    private int time=1;
    private SourceA s;
    private String id = "001";
    public TestRunnable(SourceA s){
        this.s = s;
    }
    public void setTime(int time) {
        this.time = time;
    }
    
    @Override
    public void run() {
        try {
            System.out.println("i will sleep"+ time);
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        synchronized(s){
            s.notify();
            System.out.println("我唤醒了002!");
            System.out.println("我存入了id"+id);
            s.setSource(id);
        }
    }

}

Thread 2: Next, use the method of inheriting Thread to implement multi-threading:.

public class TestThread extends Thread {
    private int time = 1;
    private SourceA s = null;
    String id = "002";
    
    public void setTime(int time) {
        this.time = time;
    }
    
    public TestThread(SourceA s){
        this.s = s ;
    }
    
    @Override
    public void run() {
        try {
            System.out.println("i will sleep"+ time);
            sleep(time);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        synchronized(s){
            try {
                System.out.println("我"+ id +"要进行等待了");
                s.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("我被唤醒了");
            System.out.println("我存入了id"+id);
            s.setSource(id);
        }
    }

}

The class that synchronizes threads has get and set methods.

public class SourceA {
    private List<String> list = new ArrayList<String>();
    public synchronized void getSource(){
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }
    }
    public synchronized void setSource(String id){
        list.add(id);
    }
}

Test class, print multi-threaded results to the console.

public void test(){
        SourceA s = new SourceA();
        TestThread tt = new TestThread(s);
        TestRunnable tr = new TestRunnable(s);
        Thread t = new Thread(tr);
        System.out.println("调用线程1");
        tt.start();
        System.out.println("调用线程2");
        t.start();
    }

The above is the detailed content of What is Java multithreading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is swing in javaNext article:What is swing in java