程序:(program)是一个指令的集合
进程:Process,(正在执行中的程序)是一个静态的概念。进程是程序的异常静态执行过程,占用特定的地址空间,每个进程都是独立的,有3部分组成cup,data,code
缺点:内存的浪费,CPU的负担
线程:是进程中一个“单一的连续控制流程”。CPU调度的是线程
1.继承Thread类
2.重写run()方法
3.通过start()方法启动线程
1 package com.zqf.threadproject; 2 3 public class Test { 4 public static void main(String[] args) { 5 MyThread my = new MyThread(); 6 my.start(); //启动线程 7 System.out.println("-------main"); 8 } 9 }
1 package com.zqf.threadproject; 2 3 public class MyThread { 4 5 public void start() { 6 // TODO Auto-generated method stub 7 //线程体 8 System.out.println("MyThread-----"); 9 } 10 }
1 package com.zqf.runnableproject; 2 3 public class test1 { 4 public static void main(String[] args) { 5 //创建线程类的对象 6 MyRunnable my=new MyRunnable(); 7 //start()方法是Thread类中的方法 8 Thread t = new Thread(my); 9 t.start(); //启动线程 10 //主线程中的循环 11 for(int i=0;i<10;i++){ 12 System.out.println("-------mian()"); 13 } 14 } 15 }
1 package com.zqf.runnableproject; 2 3 public class MyRunnable implements Runnable{ //具备了多线程操作能力 4 5 @Override 6 public void run() { 7 for(int i=0;i<10;i++){ 8 System.out.println("MyRunnable.run()-----"+i); 9 } 10 } 11 }
原文:https://www.cnblogs.com/zqfdgzrc/p/10653150.html