多线程
进程和线程
进程:具有一定独立功能的程序关于某个数据集合上的一次进行活动,进程是系统进行资源分配和调度的一个独立单位。
线程:是进程的一个实体,是CPU调度和分配的基本单位
线程
如何创建线程
代码展示
class Demo{
public static void main(String [] args){
new Thread1().start();
new Thread(new Runnable1()).start();
}
}
// 方法一:
class Thread1 extends Thread{
public void run(){
System.out.println("线程 ---- 1");
}
}
// 方法二:
class Runnable1 implements Runnable{
public void run(){
System.out.println("线程 ---- 2");
}
}
run函数和start函数的区别
run函数:复写run函数,则是为了自定义功能,直接执行run函数,只会执行这个函数的功能,该功能只会执行在当前线程,不会执行在新创建的线程中
start函数:在新创建的线程中执行run函数的功能
多线程安全问题
当多个线程处理同一个数据的时候,会出现多线程安全问题
如下
class Demo{
public static void main(String [] args){
// 多线程安全问题
TaskRunnable taskRunnable = new TaskRunnable();
Thread t1 = new Thread(taskRunnable);
Thread t2 = new Thread(taskRunnable);
Thread t3 = new Thread(taskRunnable);
Thread t4 = new Thread(taskRunnable);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TaskRunnable implements Runnable{
private int ticketCount = 100;
public void run(){
while(true){
if(ticketCount > 0){
try{
Thread.sleep(10);
}catch(Exception e){
}
System.out.println(Thread.currentThread().getName()+" == 买票 "+ticketCount--);
}
}
}
}
解决方法:同步代码块
class Demo{
public static void main(String [] args){
// 多线程安全问题
TaskRunnable taskRunnable = new TaskRunnable();
Thread t1 = new Thread(taskRunnable);
Thread t2 = new Thread(taskRunnable);
Thread t3 = new Thread(taskRunnable);
Thread t4 = new Thread(taskRunnable);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TaskRunnable implements Runnable{
private int ticketCount = 100;
private Object object = new Object();
public void run(){
while(true){
// 同步代码锁:
// 1.优点:解决了线程安全的问题
// 2.缺点:多线程需要判断锁,比较消耗资源
synchronized(object){
if(ticketCount > 0){
try{
Thread.sleep(10);
}catch(Exception e){
}
System.out.println(Thread.currentThread().getName()+" == 买票 "+ticketCount--);
}
}
}
}
}
同步函数
class Demo{
public static void main(String [] args){
// 多线程安全问题
TaskRunnable taskRunnable = new TaskRunnable();
Thread t1 = new Thread(taskRunnable);
Thread t2 = new Thread(taskRunnable);
Thread t3 = new Thread(taskRunnable);
Thread t4 = new Thread(taskRunnable);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
class TaskRunnable implements Runnable{
private int ticketCount = 100;
private Object object = new Object();
public void run(){
while(true){
saleTicket();
}
}
// 同步函数的锁就是当前类本身 this
public synchronized void saleTicket(){
if(ticketCount > 0){
try{
Thread.sleep(10);
}catch(Exception e){
}
System.out.println(Thread.currentThread().getName()+" == 买票 "+ticketCount--);
}
}
}
死锁
class Demo{
public static void main(String [] args){
// 多线程安全问题
LockThread lockThread1 = new LockThread(true);
LockThread lockThread2 = new LockThread(false);
lockThread1.start();
lockThread2.start();
}
}
class LockThread extends Thread{
boolean flag = false;
public LockThread(boolean flag){
this.flag = flag;
}
public void run(){
if(flag){
while(true){
synchronized(Lock.lockA){
System.out.println("if locka");
synchronized(Lock.lockB){
System.out.println("if lockb");
}
}
}
}else{
while(true){
synchronized(Lock.lockB){
System.out.println("if lockb");
synchronized(Lock.lockA){
System.out.println("if locka");
}
}
}
}
}
}
class Lock{
public static Object lockA = new Object();
public static Object lockB = new Object();
}
线程间通信
wait,notify,notifyAll的使用
class Demo{
public static void main(String [] args){
// 线程间通信
Resource res = new Resource();
Producter producter = new Producter(res);
Customer customer = new Customer(res);
Thread thread1 = new Thread(producter);
Thread thread2 = new Thread(producter);
Thread thread3 = new Thread(customer);
Thread thread4 = new Thread(customer);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class Resource{
private boolean flag = false;
private String name;
private int count = 1;
public synchronized void set(String name){
while(flag){
try{
this.wait();
}catch(Exception e){
}
}
this.name = name + "----" +count++;
System.out.println(Thread.currentThread().getName()+"... 生产者..."+this.name);
flag = true;
this.notifyAll();
}
public synchronized void out(){
while(!flag){
try{
this.wait();
}catch(Exception e){
}
}
System.out.println(Thread.currentThread().getName()+"... 消费者..."+this.name);
flag = false;
this.notifyAll();
}
}
class Producter implements Runnable{
private Resource res;
public Producter(Resource res){
this.res = res;
}
public void run(){
while(true){
res.set("张三");
}
}
}
class Customer implements Runnable{
private Resource res;
public Customer(Resource res){
this.res = res;
}
public void run(){
while(true){
res.out();
}
}
}
Lock,Condition
import java.util.concurrent.locks.*;
class Demo{
public static void main(String [] args){
// 线程间通信
Resource res = new Resource();
Producter producter = new Producter(res);
Customer customer = new Customer(res);
Thread thread1 = new Thread(producter);
Thread thread2 = new Thread(producter);
Thread thread3 = new Thread(customer);
Thread thread4 = new Thread(customer);
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class Resource{
private boolean flag = false;
private String name;
private int count = 1;
private Lock lock = new ReentrantLock();
private Condition customerCondition = lock.newCondition();
private Condition producterCondition = lock.newCondition();
public void set(String name){
lock.lock();
try{
while(flag){
producterCondition.await();
}
this.name = name + "----" +count++;
System.out.println(Thread.currentThread().getName()+"... 生产者..."+this.name);
flag = true;
customerCondition.signal();
}catch(Exception e){
}finally{
lock.unlock();
}
}
public synchronized void out(){
lock.lock();
try{
while(!flag){
customerCondition.await();
}
System.out.println(Thread.currentThread().getName()+"... 消费者..."+this.name);
flag = false;
producterCondition.signal();
}catch(Exception e){
}finally{
lock.unlock();
}
}
}
class Producter implements Runnable{
private Resource res;
public Producter(Resource res){
this.res = res;
}
public void run(){
while(true){
res.set("张三");
}
}
}
class Customer implements Runnable{
private Resource res;
public Customer(Resource res){
this.res = res;
}
public void run(){
while(true){
res.out();
}
}
}