假如我现在需要在读数据库的同时往磁盘里写数据,这个要怎么做呢?
package com.thread.thread01;
/**
 * @program: ThreadDemo
 * @description: 创建并启动线程
 * @author: hs96.cn@Gmail.com
 * @create: 2020-08-26 09:56
 */
public class TryConcurrency {
    public static void main(String[] args) {
        readFromDataBase();
        writeDataToFile();
    }
    /**
     * 读数据
     */
    private static void readFromDataBase() {
        // read data from database and handle it
        try {
            println("Begin read data from db.");
            Thread.sleep(1000 * 1L);
            println("Read data done and start handle it.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        println("The data handle finish and successfully.");
    }
    /**
     * 写数据
     */
    private static void writeDataToFile() {
        // write data to file
        try {
            println("Begin write data to file.");
            Thread.sleep(1000 * 1L);
            println("Write data done and start handle it.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        println("The data handle finish and successfully.");
    }
    private static void println(String message) {
        System.out.println(message);
    }
}
运行效果如下:

可以看到这两个方法其实是顺序执行的不是交替执行的,接下来我们打开JDK的文档,找到Thread类:

可以看到在JVM启动的时候其实有一个非守护的main线程来启动我们的main函数。
为了验证这个,我们来创建一个sleep的线程,我们来使用jconsole工具来查看一下:
public static void main(String[] args) {
    try {
        Thread.sleep(1000 * 100L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}




可以看到是有一个main线程存在的,他是waiting状态,所以,我们可以在main方法里,再创建一个线程,让这两个线程交替打印一下数字:
public static void main(String[] args) {
       Thread t1 = new Thread("Custom-Thread") {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    println("Task i=>" + i);
                    try {
                        Thread.sleep(1000 * 1L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t1.start();
        for (int j = 0; j < 1000; j++) {
            try {
                Thread.sleep(900 * 1L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            println("Task j=>" + j);
        }
    }
效果如下:

可以看到这两个线程已经交替执行了。
再用jconsole查看一下:

把main线程的循环注释掉:
public static void main(String[] args) {
    Thread t1 = new Thread("Custom-Thread") {
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                println("Task i=>" + i);
                try {
                    Thread.sleep(1000 * 1L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t1.start();
    /*for (int j = 0; j < 1000; j++) {
        try {
            Thread.sleep(900 * 1L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        println("Task j=>" + j);
    }*/
}
再用jconsole查看一下:

main线程已经退出了。
还有一个容易忽略的问题:

显然不是,因为执行结果直接就出来了:

这里涉及到线程的生命周期了,后续再深入学习,这里只提一下。
现在再思考之前提出的问题:假如我现在需要在读数据库的同时往磁盘里写数据,这个要怎么做呢?
/**
 * @program: ThreadDemo
 * @description: 创建并启动线程
 * @author: hs96.cn@Gmail.com
 * @create: 2020-08-26 09:56
 */
public class TryConcurrency {
    public static void main(String[] args) {
        new Thread("READ-Thread") {
            @Override
            public void run() {
                readFromDataBase();
            }
        }.start();
        new Thread("WRITE-Thread") {
            @Override
            public void run() {
                writeDataToFile();
            }
        }.start();
    }
    /**
     * 读数据
     */
    private static void readFromDataBase() {
        // read data from database and handle it
        try {
            println("Begin read data from db.");
            Thread.sleep(1000 * 1L);
            println("Read data done and start handle it.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        println("The data handle finish and successfully.");
    }
    /**
     * 写数据
     */
    private static void writeDataToFile() {
        // write data to file
        try {
            println("Begin write data to file.");
            Thread.sleep(1000 * 1L);
            println("Write data done and start handle it.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        println("The data handle finish and successfully.");
    }
    private static void println(String message) {
        System.out.println(message);
    }
}
执行效果如下:

可以看到已经交替执行了。
再用jconsole查看一下:

这样我们就简单的实现了读数据库的同时往磁盘里写数据的操作。当然虽然没真正的度数据库和往磁盘里写数据,但是,主要还是理解这个交替执行的过程。
原文:https://www.cnblogs.com/stormsquirrel/p/13570614.html