进程和线程
进程
- 进程:是正在执行的程序,是资源分配的基本单位,具有独立的地址空间
- 操作系统会为其分配cpu和内存
线程
- 线程:引入线程是为了解决进程开销大,浪费资源的情景,并且多进程并发效率比较低
- 线程是调度执行的基本单位
- 线程之间会相互影响,一个线程挂了,会影响到整个进程都异常结束,线程也自然会结束
进程和线程的区别
- 进程包含线程,一个进程里面有多个线程或者是一个线程
- 进程和线程都是用来实现并发编程场景的,但是线程比进程更轻量和高效
- 同一个进程的线程之间共用同一份资源(内存和硬盘),省去了申请资源的开销
- 进程和进程之间都是独立存在的,不会相互影响,同一个进程中,线程和线程之间会相互影响(线程安全问题 + 线程出现异常)
- 进程是分配资源的基本单位,线程是调度执行的基本单位
创建线程的五种写法
继承thread,重写run
package thread; class mythread extends thread{ public void run(){ // 这个是线程的入口方法 while(true) { system.out.println("hello thread!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } } } // 创建线程 public class demo1 { public static void main(string[] args) throws interruptedexception { thread thread = new mythread(); // 使用start方法可以间接调用run方法 // start和 run都时thread的成员 // run 只是线程的入口(描述了线程要做什么事情) // start才是真正调用了系统的api,在系统中创建出了线程,让线程调用 run thread.start(); // 从这句开始程序就并发执行,一边执行hello main,一边执行hello thread // 兵分两路进行执行 // 并发 == 并行 + 并发 while(true){ system.out.println("hello main!"); thread.sleep(1000); } // 先执行main,再执行的是thread,先执行主线程 } }
实现runnable(接口),重写run
package thread; class myrunable implements runnable{ public void run(){ while(true){ system.out.println("hello thread!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } } } public class demo2 { public static void main(string[] args) throws interruptedexception { // 这个接口就是用来实现多态的 runnable myrunable = new myrunable(); thread thread = new thread(myrunable); thread.start(); while(true){ system.out.println("hello main!"); thread.sleep(1000); } } }
继承thread,重写run,但是使用匿名内部类
- 使用匿名内部类的方式创建出线程
package thread; public class demo3 { public static void main(string[] args) { thread thread = new thread(){ public void run(){ while(true){ system.out.println("hello thread!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } } }; thread.start(); while(true){ system.out.println("hello main!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } } }
实现runnable(接口),重写run,但是使用匿名内部类
package thread; public class demo4 { public static void main(string[] args) { // 法一:创建实例 runnable runnable = new runnable(){ public void run(){ system.out.println("hello thread!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } }; // 法二:创建匿名对象 thread thread = new thread(new runnable(){ public void run(){ system.out.println("hello thread!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } }); // thread thread = new thread(runnable); thread.start(); while(true){ system.out.println("hello main!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } } }
使用lambda表达式
- lambda表达式相当于是匿名内部类的替换写法
package thread; public class demo5 { public static void main(string[] args) { thread thread = new thread(()->{ while(true){ system.out.println("hello thread!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } }); thread.start(); while(true){ system.out.println("hello main!"); try { thread.sleep(1000); } catch (interruptedexception e) { throw new runtimeexception(e); } } } }
请说明thread类中run和start的区别
从方法的区别,及运行结果的区别分别说明
1. start方法可以用来启动一个新的线程,run方法只是一个普通的方法,在主线程中执行
2.start方法只能调用一次,run方法可以调用多次
3.调用start方法会执行新的线程,新线程和主线程并发执行,run方法只是线程的入口,start方法调用了系统api,start方法创建出了线程,让线程再调用run方法
4.run方法和主线程同步执行,start方法启动的线程和主线程异步执行
5.run方法按顺序执行,start方法调用的线程中的代码执行顺序由线程调度器决定,顺序不确定
到此这篇关于javaee多线程之进程和线程之间的区别和联系(最新整理)的文章就介绍到这了,更多相关java进程和线程区别内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论