Java 多线程再学习

进程OR线程

当一个程序运行时,他就是一个进程,一个进程可能包含多条线程。
我们一般将网络下载,后台任务放在线程中进行,不影响前端操作。

如何实现一个线程?

线程实现的方法有两种
1.继承Thread类
2.实现Runable接口,传入Thread当中
或者有人写的第三种,实现Runable的匿名内部类

线程之间通信

在锁这个问题上需要注意的是synchronized其中的Object是其共同资源。锁的目的是锁住共同资源,根据共同资源在线程之间进行通信,比如wait(),notify(),notifyAll();

参考代码部分

Main

import java.util.ArrayList;

public class ThreadTest {
    public static void main(String[] args) {
        ArrayList arrayList=new ArrayList();
        Object object=new Object();
        PutThread putThread=new PutThread(arrayList,object);
        GetThread getThread=new GetThread(arrayList,object);
        GetThread2 getThread2=new GetThread2(arrayList,object);
        Thread put=new Thread(putThread);
        Thread get=new Thread(getThread);
        Thread get2=new Thread(getThread2);
        get.start();
        get2.start();
        put.start();

    }
}

GetThread1

import java.util.ArrayList;

public class GetThread extends Thread {
    ArrayList arrayList = null;
    int max = 1;
    Object object=null;

    public GetThread(ArrayList arrayList,Object object) {
        this.arrayList = arrayList;
        this.object=object;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("GET-----------1 "+arrayList.size());
            synchronized (object) {
                if (arrayList.size() < max) {
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {

                    System.out.println("GET1取出成功" + arrayList.remove(0) + "个");
                    System.out.println("GET-----------1 "+arrayList.size());
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    object.notify();
                }
            }
        }
    }
}

GetThread2

import java.util.ArrayList;

public class GetThread2 extends Thread {
    ArrayList arrayList = null;
    int max = 1;
    Object object=null;

    public GetThread2(ArrayList arrayList ,Object object) {
        this.arrayList = arrayList;
        this.object=object;
    }

    @Override
    public void run() {
        while (true) {
            System.out.println("GET-----------2 "+arrayList.size());
            synchronized (object) {
                if (arrayList.size() < max) {
                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.println("GET2取出成功" + arrayList.remove(0) + "个");
                    System.out.println("GET-----------2 "+arrayList.size());
                    try {
                        sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    object.notify();
                }
            }
        }
    }
}

PutThread

import java.util.ArrayList;

public class PutThread extends Thread {
    ArrayList arrayList = null;
    Object object=null;
    int max = 1;
    int count = 1;

    public PutThread(ArrayList arrayList,Object object) {
        this.arrayList = arrayList;
        this.object=object;
    }

    @Override
    public void run() {
            while (true) {
                synchronized (object) {
                if (arrayList.size() >=max) {

                    try {
                        object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }else {
                    arrayList.add(count);
                    System.out.println("放入第" + count + "个");
                    count++;
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    object.notify();
                }
            }
        }
    }
}

写在最后

线程的同步通信,其资源锁问题之间的通信是多线程的关键。

你可能感兴趣的:(JAVA)