The beginning of multithreading

Time:2020-10-25

Create thread

To understand multithreading, you must first know how to create a thread. There are three ways to create a thread: inherit the thread class, implement the runnable interface, and implement the callable interface.

//Create thread mode 1: inherit thread class, override run() method, call start to start thread
public class TestThread1 extends Thread {
    @Override
    public void run() {
        //Run method thread body
        for (int i = 0; i < 20; i++) {
            System.out.println("I'm learning"+i);
        }
    }

    public static void main(String[] args) {
        //Main thread

        //Create a thread object
        TestThread1 testThread1 = new TestThread1();
        //Call the start() method to open the method
        testThread1.start();

        for (int i = 0; i < 20; i++) {
            System.out.println("I'm learning multithreading"+i);
        }
    }
}
//Create thread mode 2: implement the runnable interface and rewrite the run method. The execution thread needs to drop the runnable interface implementation class and call the start method
public class TestThread3 implements Runnable{
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println("I'm looking at the code");
        }
    }

    public static void main(String[] args) {

        //Creating implementation class object of runnable interface
        TestThread3 testThread3 = new TestThread3();
        //Create thread object, open our thread and proxy through thread object
        Thread thread = new Thread(testThread3);

        thread.start();

        for (int i = 0; i < 20; i++) {
            System.out.println("I'm learning multithreading");
        }
    }
}

The third way is to use a downloaded jar package to demonstrate. Import the dependency first

<dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.7</version>
        </dependency>
</dependencies>
//The third way of thread creation is to realize callable interface
//Import a dependency first
/ *
public class TestCallable implements Callable<Boolean> {

    private String url;
    private String name;

    public TestCallable(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public Boolean call() throws Exception {
        webDownloader webDownloader = new webDownloader();
        webDownloader.downloader(url,name);
        System.out.println("Downloaded file for"+name);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable("Picture address", "1.jpg");
        TestCallable t2 = new TestCallable("Picture address", "2.jpg");
        TestCallable t3 = new TestCallable("Picture address", "3.jpg");

        //Create execution service:
        ExecutorService ser = Executors.newFixedThreadPool(3);
        //Submit execution service
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);

        //Get the results
        Boolean rs1 = r1.get();
        Boolean rs2 = r2.get();
        Boolean rs3 = r3.get();

        //Shut down the service
        ser.shutdownNow();
    }
}


//Downloader
class webDownloader{

    public void downloader(String url,String name) {
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("I0 exception, there is a problem with the downloader method");
        }
    }

}

Thread unsafe

Deadlock condition

  • Mutex condition: a resource can only be used by one process at a time.
  • Request and hold condition: when a process is blocked by a request for resources, it holds the acquired resources.
  • The condition of not depriving: the resources obtained by the process can not be forcibly deprived before they are used up.
  • Cyclic waiting condition: a cyclic waiting resource relationship is formed between several processes.