Multithreading-thread creation (1)

Multithreading-thread creation (1)

  1. Program: It is an ordered collection of instructions and data. It does not have any operational meaning. It is a shocking concept.

  2. Process: It is an execution process of executing a program. It is a dynamic concept and a unit of system resource allocation. Usually, a process can contain a number of threads. Of course, there is at least one thread in a process, otherwise it has no meaning. The thread is the unit of CPU scheduling and execution.

  3. Thread: Thread is an independent execution path

    • When the program is running, even if you do not create a thread, there will be multiple threads in the background, such as the main thread and gc thread;
    • main() is called the main thread, which is the entry point of the system and is used to execute the entire program
    • In a process, if multiple threads are opened up, the running of the threads will be scheduled by the scheduler. The scheduler is closely related to the operating system, and the order cannot be interfered by human intervention.
    • When operating on the same resource, there will be a problem of resource grabbing, and concurrency control needs to be added
    • Threads will bring additional overhead, such as cpu scheduling time, concurrency control overhead
    • Each thread interacts in its own working memory. Improper memory control will cause data inconsistency.

    Common method calls and multithreading:

Insert picture description here

  1. Method one of thread creation: Threadclass inherits Thread class (emphasis)

    //创建线程方式一:继承Thread类,重写run方法,调用start开启线程
    
    //总结:注意,线程开启不一定立即执行,由CPU调度执行
    public class TestThread1 extends Thread{
          
          
        @Override
        public void run() {
          
          
            //run方法线程体
            for (int i = 0; i < 200; i++) {
          
          
                System.out.println("我在看代码--------"+i);
            }
        }
    
        public static void main(String[] args) {
          
          
            //main线程,主线程
            //创建一个多线程对象
            TestThread1 testThread1=new TestThread1();
    
            //调用start()方法开启线程
            testThread1.start();//start()是同时执行的
    
            for (int i = 0; i < 1000; i++) {
          
          
                System.out.println("我在学习多线程--"+i);
            }
        }
    }
    
    
  2. Download the picture and contact Thread

    • First, download the commons-io package and import this jar package into the project. For details, please refer to the download of Apache IO package . Our code needs to use the copyURLToFile() method of the FileUtils class (copy a URL to a file).

    • Find out the address of the picture you want to download to facilitate our code implementation.

Insert picture description here

  • Specific code implementation

    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    import java.net.URL;
    
    //练习Thread,实现多线程同步下载图片
    public class TestThread2 extends Thread {
          
          
        private String url;//网络图片地址
        private String name;//保存的文件名
    
        public TestThread2(String url,String name){
          
          
            this.url=url;
            this.name=name;
        }
    
        //下载图片的执行体
        @Override
        public void run() {
          
          
            WebDownloader webDownloader=new WebDownloader();
            webDownloader.downloader(url,name);
            System.out.println("下载了文件名为:"+name);
        }
    
        public static void main(String[] args) {
          
          
            TestThread2 t1=new TestThread2("https://img-blog.csdnimg.cn/20210316213155604.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1h1bl9pbmRlcGVuZGVudA==,size_16,color_FFFFFF,t_70#pic_center","1.jpg");
            TestThread2 t2=new TestThread2("https://img-blog.csdnimg.cn/20210316213209625.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1h1bl9pbmRlcGVuZGVudA==,size_16,color_FFFFFF,t_70#pic_center","2.jpg");
            TestThread2 t3=new TestThread2("https://img-blog.csdnimg.cn/20210316213228816.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1h1bl9pbmRlcGVuZGVudA==,size_16,color_FFFFFF,t_70#pic_center","3.jpg");
    
            //先下载t1
            t1.start();
            //然后是t2
            t2.start();
            //最后是t3
            t3.start();
            //但是事实不是按顺序的,start()是同时进行的,谁先下载要取决于cpu的安排调度
        }
    }
    
    //下载器
    class WebDownloader{
          
          
        //下载方法
        public void downloader(String url,String name){
          
          
            try {
          
          
                //copyURLToFile()拷贝一个URL到文件
                FileUtils.copyURLToFile(new URL(url),new File(name));
            } catch (IOException e) {
          
          
                e.printStackTrace();
                System.out.println("IO异常,downloader方法出现问题");
            }
        }
    }
    
  • Picture downloaded successfully

Insert picture description here

Guess you like

Origin blog.csdn.net/Xun_independent/article/details/114938880