进程基础介绍:
进程:
进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。
线程:
有时被称为轻量级进程(Lightweight Process,LWP),是程序执行流的最小单元。
线程本身不能单独运行,必须在一个进程中运行。
java的线程模型:
- 新建状态:线程被创建之后便处于新建状态
- 就绪状态:
- 新建的线程调用start()进入就绪状态
- 阻塞的线程解除阻塞状态进入就绪状态
- 阻塞状态:是一种正在运行的线程因为一些原因让出cpu资源时终止而进入的状态
- 终止状态:正常、异常或强制终止的线程状态
- 可以使用destory()、方法强制终止
创建线程:
1.继承Thread类:
为实现线程的并发,不能直接使用对象调用自己的方法,要使用start()方法开始;
如:
public class text {
public static void main(String[] args){
MyThread mt = new MyThread();
mt.start();
for (int i=0; i<10; i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("main线程"+(i+1));
}
System.out.println("线程执行完毕!");
}
}
public class MyThread extends Thread {
public void run(){
for(int i=0; i<10;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("副线程"+(i+1));
}
System.out.println("没救了");
}
}
实现Runnable接口
如:
public class MyRunnable implements Runnable {
@Override
public void run() {
for (int i=1; i<=10;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程"+i);
}
System.out.println("执行完毕");
}
}
public class RunnableMain {
public static void main(String[] args){
MyRunnable mr = new MyRunnable();
Thread xc = new Thread(mr);
xc.start();
for (int i=1; i<=10; i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("主线程"+i);
}
System.out.println("没救了");
}
}
两种方法比较:java为单继承,不能继承多个类,继承Thread不易拓展,但接口Runnable也是依赖于第一种方法;
多个程序并发执行:
多个线程启动后,没有意外则每个线程都执行,但是不能确定那个哪个先运行
线程优先级:
java中优先级高的线程有更大可能性获得cpu,但不是优先级高的总是先执行,也不是优先级低的线程总是后执行
在java中,线程优先级分十个等级,1-10;默认的优先级为5;
改变线程优先级:
public static void main(String[] args){
XianCheng2 xc2 = new XianCheng2();
XianCheng1 xc1 = new XianCheng1();
Thread t1 = new Thread(xc1);
Thread t2 = new Thread(xc2);
System.out.println("t1的优先级:"+t1.getPriority());
System.out.println("t2的优先级:"+t2.getPriority());
//也可以直接传入1-10的数字
//t1.setPriorty(3);
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
System.out.println("t1改变后的优先级:"+t1.getPriority());
System.out.println("t2改变后的优先级:"+t2.getPriority());
}
线程调度的三个常见方法:
- 睡眠 sleep(毫秒数,纳秒数);
- 暂停 yield(); 释放资源供所有就绪线程(包括自己)竞争
- 挂起 join();
线程同步:
public class printScore {
public void printScoreing(String name,int EScore,int CScore,int MScore){
System.out.println(name+"的英语成绩:"+EScore);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"的语文成绩:"+CScore);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"的数学成绩:"+MScore);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Teacher implements Runnable{
private printScore p;
private String name;
private int EScore;
private int CScore;
private int MScore;
public Teacher(printScore p,String name,int EScore,int CScore,int MScore){
this.p=p;
this.name=name;
this.CScore=CScore;
this.EScore=EScore;
this.MScore=MScore;
}
@Override
public void run() {
p.printScoreing(name,EScore,CScore,MScore);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class PrintMain {
public static void main(String[] args){
printScore p = new printScore();
Teacher t1 = new Teacher(p,"ljb",100,100,100);
Teacher t2 = new Teacher(p,"QHY",99,89,25);
Teacher t3 = new Teacher(p,"znd",71,89,32);
Thread T1 = new Thread(t1);
Thread T2 = new Thread(t2);
Thread T3 = new Thread(t3);
T1.start();
T2.start();
T3.start();
}
}
ljb的英语成绩:100
znd的英语成绩:71
QHY的英语成绩:99
ljb的语文成绩:100
znd的语文成绩:89
QHY的语文成绩:89
ljb的数学成绩:100
QHY的数学成绩:25
znd的数学成绩:32
如此运行的结果比较杂乱,则需要用synchronized同步
- 同步方法:
用synchronized修饰符修饰即可:
如:
public synchronized void printScoreing(String name,int EScore,int CScore,int MScore){
System.out.println(name+"的英语成绩:"+EScore);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"的语文成绩:"+CScore);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(name+"的数学成绩:"+MScore);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- 同步代码块:
如:
synchronized(要控制的资源){
//要执行的代码块
}
synchronized(p){
p.printScoreing(name,EScore,CScore,MScore);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
网友评论