zoukankan      html  css  js  c++  java
  • Java_多线程

    进程和线程

    在程序运行时,即使自己没有创建线程,也会默认有main主线程,以及gc线程(main线程是调用整个程序的主入口,gc线程是jvm里的垃圾回收机制)

     

    注意:线程开始不一定执行,由cpu调度执行

     

    线程的创建方式:

    1.继承Thread类,实现run()方法              *(不推荐)

    2.实现runnable接口,实现run()方法        *(推荐)

    3.实现callable接口,                             

     

    创建的第一种方式

     1.继承Thread类,重写run()方法,调用start()方法开启线程

     

     案例1:

     1 package com.thread.create;
     2 
     3 // 创建线程的第一种方式: 继承Thread类,重写run()方法,调用start()方法开启线程
     4 public class ThreadCreateForm01 extends Thread{
     5     @Override
     6     public void run() {
     7 
     8         for (int i = 0; i < 20; i++) {
     9             System.out.println("我是重写的run()的线程" + i);
    10         }
    11     }
    12 
    13     public static void main(String[] args) {
    14         // 创建线程的对象
    15         ThreadCreateForm01 thread01 = new ThreadCreateForm01();
    16         // 开启线程
    17         thread01.start();
    18 
    19         // 程序的主线程main()方法
    20         for (int i = 0; i < 500; i++) {
    21             System.out.println("我是main()线程" + i);
    22         }
    23     }
    24 }

     

    案例:下载图片 代码

     1 package com.thread.create;
     2 
     3 import org.apache.commons.io.FileUtils;
     4 
     5 import java.io.File;
     6 import java.io.IOException;
     7 import java.net.URL;
     8 
     9 public class ImageDown extends Thread{
    10 
    11     private String url;
    12     private String name;
    13 
    14 
    15     public ImageDown() {
    16     }
    17 
    18     public ImageDown(String url, String name) {
    19         this.url = url;
    20         this.name = name;
    21     }
    22 
    23     @Override
    24     public void run() {
    25         WebImageDown webImageDown = new WebImageDown();
    26         webImageDown.downLoader(url,name);
    27         System.out.println("下载了图片"+name);
    28     }
    29 
    30     public static void main(String[] args) {
    31         ImageDown thread1 = new ImageDown("https://pic.cnblogs.com/avatar/812339/20150919164020.png","thread1.png");
    32         ImageDown thread2 = new ImageDown("https://pic.cnblogs.com/avatar/812339/20150919164020.png","thread2.png");
    33         ImageDown thread3 = new ImageDown("https://pic.cnblogs.com/avatar/812339/20150919164020.png","thread3.png");
    34         thread1.start();
    35         thread2.start();
    36         thread3.start();
    37     }
    38 
    39 }
    40 
    41 // 下载器
    42 class WebImageDown{
    43     // 下载方法
    44     public void downLoader(String url, String name){
    45         try {
    46             FileUtils.copyURLToFile(new URL(url), new File(name));
    47         } catch (IOException e) {
    48             e.printStackTrace();
    49             System.out.println("IO异常:下载文件报错");
    50         }
    51     }
    52 }

     先在lib下面导入 commons.io 包,

    运行结果

     

    创建的第二种方式

    2.实现Runnable接口,重写run()方法,执行线程需要丢入Runnable接口实现类,调用start()方法 

    创建的代码

     1 package com.thread.create;
     2 
     3 // 创建线程的第二种方式:实现Runnable接口,重写run()方法,执行线程需要丢入Runnable接口实现类,调用start()方法
     4 public class ThreadCreateForm02 implements Runnable{
     5     @Override
     6     public void run() {
     7         for (int i = 0; i < 20; i++) {
     8             System.out.println("实现Runnable接口,重写的run()方法" + i);
     9         }
    10     }
    11 
    12     public static void main(String[] args) {
    13         ThreadCreateForm02 thread01 = new ThreadCreateForm02();
    14         // Thread thread = new Thread(thread01);
    15         // thread.start();
    16         // 上面两句简写为:
    17         new Thread(thread01).start();
    18 
    19         // 程序的主线程main()方法
    20         for (int i = 0; i < 1000; i++) {
    21             System.out.println("我是main()线程" + i);
    22         }
    23     }
    24 }

    运行结果:

     小结:

     

  • 相关阅读:
    两年工作感想
    ASP常用的38个内置函数
    asp汉字转换成汉语拼音
    js高级表格排序
    使用XmlHttpRequest对象调用Web Services 服务
    75个最佳Web设计资源
    C# FTP操作类
    存储过程操作类
    Windows Mobile 5.0 SDK 下载地址
    链表C#实现
  • 原文地址:https://www.cnblogs.com/1123-wyl/p/15014951.html
Copyright © 2011-2022 走看看