线程 同步 死锁

7 线程  Thread   

1)线程和进程的区别:线程是比进程更小的单位,一个线程就是一个在CPU队里排队的任务

2)run 和 start的区别,run方法只是做简单的方法调用,不能开启一个新的时间分支,start可以开启一个新的线程

3)创建线程的两种方式:

  (1)继承Thread类,并重写run方法

    //main主线程
public static void main(String[] args) {
//start进行多任务,并行执行,开启一个新线程
// MyThread01 th01=new MyThread01();
// th01.run();//阻塞式执行
//// th01.start();
//将该线程要执行的代码放在run里
public class MyThread01 extends Thread {
@Override
public void run() {
for(int i=0;i<1000;i++){
System.out.println("------------------");
}
}
}

 

  (2)实现Runnable接口,并重新run方法(可以实现多继承)

////        MyThread04 th04=new MyThread04();
//// Thread th=new Thread(th04);
//// th.start();
public class MyThread04 implements Runnable {
@Override
public void run() {
for (int i=0;i<1000;i++){
System.out.println("========");
}
}
}

 

两种方法的区别:只有Thread和他的子类有start方法

public class ThreadForTime implements Runnable {
boolean flag=true;//标志位

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

@Override
public void run() {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (flag){
System.out.println(sdf.format(new Date()));
try {
Thread.sleep(1000);//相当于睡眠状态,不出让cpu
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("---------end-------");

}
}
public class DemoForTime {//线程结束不能用stop
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ThreadForTime thread=new ThreadForTime();
Thread th=new Thread(thread);
th.start();
String c=sc.next();
if("0".equals(c)){
thread.setFlag(false);
}
}
}

4)线程同步(synchronized):确保一段代码在执行时,不被其他线程打乱
(1)同步方法
public class Th02 implements Runnable {
int count=0;
Boolean flag=true;
@Override
public void run() {
while (flag) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
sale();
}

}
public synchronized void sale () {
if (count <= 100) {
System.out.println(Thread.currentThread() + ":" + count);
count++;
} else {
flag = false;

}
}
}
Th02 th=new Th02();
Thread thread=new Thread(th);
thread.start();
Thread thread1=new Thread(th);
thread1.start();

(2)同步代码块


public class ThreadForClock implements Runnable {
boolean flag=true;
int count=0;
boolean isSuspend=false;
Object ob=new Object();
@Override
public void run() {
while (flag){
synchronized (ob){//括号里任意一个对象
try {
Thread.sleep(1000);
count++;
System.out.println(count);
if(isSuspend){
ob.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("-----end-----");
}

public void resume(){
synchronized (ob){
ob.notifyAll();
}
}

public boolean isFlag() {
return flag;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public boolean isSuspend() {
return isSuspend;
}

public void setSuspend(boolean suspend) {
isSuspend = suspend;
}

public Object getOb() {
return ob;
}

public void setOb(Object ob) {
this.ob = ob;
}
}
public class DemoForClock {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("1 start 2 suspend 3resume 4stop");
ThreadForClock th=new ThreadForClock();
Thread thread=new Thread(th);
while (true){
String c=sc.next();
if("1".equals(c)){
thread.start();
} else if("2".equals(c)){
th.isSuspend=true;
} else if("3".equals(c)){
th.isSuspend=false;
th.resume();
} else if("4".equals(c)){
th.setFlag(false);
break;
}
}
}
}

   5)线程通信:  wait(让线程等待一段时间,出让资源)  notify(唤醒指定一个线程)    notifyAll(唤醒所有线程)都是Object的方法,这三个方法只能用在线程同步里,在线程同步方法或者同步代码块外都不能用

  6)死锁:线程同步里

public class ThreadForDeadLock implements Runnable {//加static保证一个类 两个静态对象
private static Object o1=new Object(), o2=new Object(),o3=new Object();
private int threadCode;

public ThreadForDeadLock(int threadCode) {
this.threadCode = threadCode;
}

@Override
public void run() {
System.out.println(Thread.currentThread()+"is running");
if(threadCode==1){
synchronized (o1){//synchronized (o1)锁上了O1
System.out.println(Thread.currentThread()+"获取了o1资源");
try { //当代码块里所有代码都执行完O1才能被释放
Thread.sleep(1000);//线程启动不能控制线程速度
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2){//解锁synchronized (o3) O1执行完释放
System.out.println(Thread.currentThread()+"获取了o2资源");
}
}
}
if(threadCode==2){
synchronized (o2){//锁上了O2
System.out.println(Thread.currentThread()+"获取了o2资源");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1){
System.out.println(Thread.currentThread()+"获取了o1资源");
}
}
}
}
ThreadForDeadLock th=new ThreadForDeadLock(1);
ThreadForDeadLock th1=new ThreadForDeadLock(2);
Thread thread=new Thread(th);
thread.start();
Thread thread1=new Thread(th1);
thread1.start();




class ThreadForDeadLock类里 :
    public static Object getO1() {
return o1;
}

public static void setO1(Object o1) {
ThreadForDeadLock.o1 = o1;
}

public static Object getO2() {
return o2;
}

public static void setO2(Object o2) {
ThreadForDeadLock.o2 = o2;
}

public int getThreadCode() {
return threadCode;
}

public void setThreadCode(int threadCode) {
this.threadCode = threadCode;
}
}
posted @ 2021-03-12 16:00  YangYuJia  阅读(58)  评论(0)    收藏  举报