黑马程序员技术交流社区

标题: 笔记123 [打印本页]

作者: 命の意味    时间: 2017-12-4 16:09
标题: 笔记123
线程和进程的二三事
进程:
指某一个程序在内存中的执行区域
线程:
指某个进行中的执行单元
例如:
360软件为一个进程,其中杀毒,清理垃圾,体检等可以同时进行,为线程
多线程有三种实现方式:
第一种是子类继承Thread类重写run()方法,例如:
//定义一个Thread子类
         public class MyThreadextends Thread {
                   @override
                   public voidrun() {
                            for(inti = 0; i < 100; i++) {
                                     System.out.println(getName()+ ":" + i);
                            }
                   }
         }
         
         //测试类
         public class Test {
                   publicstatic void main(String[] args) {
                            MyThreadmt = new MyThread();
                            mt.setName("线程一");
                            mt.start();
                           
                            MyThreadmt2 = new MyThread();
                            mt2.setName("线程二");
                            mt2.start();
                           
                   }
         }
第二种是子类实现Runable接口、重写run()方法的方式:
//定义Runnable接口实现类
         public class MyThreadimplements Runnable {
                   int num ;
                   publicMyThread(int num) {
                            this.num= num;
                  }
                  
                   @override
                   public voidrun() {
                            for(inti = 0;i < num; i++) {
                                     System.out.println(Thread.currentThread().getName()+ ":" + i);
                            }
                   }
         }
         
         //测试类
         public class Test {
                   publicstatic void main(String[] args) {
                            MyThreadmt = new MyThread(100);
                            newThread(mt, "第一条").start();
                            newThread(mt, "第二条").start();
                   }
         }
第三种是子类实现Callable接口,重写call()方法:
public class Test01 {
     public static voidmain(String[] args) throws Exception{
              //创建Callable实现类对象
              MyThread mt1 =new MyThread(10);
              
              FutureTask<Integer>ft1 = new FutureTask<Integer>(mt1);
              
              new Thread(ft1,"线程一").start();
              
              int sum1 =ft1.get();
              System.out.println(sum1);
              
              
              
              MyThread mt2 =new MyThread(20);
              
              FutureTask<Integer>ft2 = new FutureTask<Integer>(mt2);
              
              new Thread(ft2,"线程二").start();
              
              int sum2 =ft2.get();
              System.out.println(sum2);
     }
}
class MyThread implements Callable<Integer> {
     int num;
     
     public MyThread(int num) {
     }
     
     @Override
     public Integer call()throws Exception {
              int sum = 0;
              for(int i = 1; i<= num; i++) {
                       Thread.sleep(200);
                       System.out.println(Thread.currentThread().getName()+ ":" + i);
                       sum +=i;
              }
              return sum;
     }
}
当然多线程还有一个安全性的问题,这个就要使用synchronized同步锁来解决,可以使用同步代码块或者同步方法的形式,这里就不多做介绍了,有兴趣的可以去查阅一下资料

作者: 奥斯托洛夫斯基    时间: 2017-12-8 15:58

作者: O-limin    时间: 2018-3-29 15:33

作者: 皖哥哥    时间: 2018-3-29 15:37
学有所得
作者: hguilin    时间: 2018-3-29 15:41
666
作者: 项老师    时间: 2018-3-29 15:43

作者: 黑马啸西风    时间: 2018-3-29 15:52

作者: 我是一匹小黑马Y    时间: 2018-3-29 16:56

作者: 美美就是美    时间: 2018-3-30 10:05

作者: 小皖妹妹    时间: 2018-3-30 19:45





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2