Day20

线程安全

1、线程局部变量的共享

定义线程安全的静态变量ConcurrentHashMap 存储线程对象的局部变量

public class Test01 {
	
	public static ConcurrentHashMap<Thread, Integer> map = new ConcurrentHashMap<>();
	
	public static void main(String[] args) {
		/**
		 * 知识点:线程局部变量的共享
		 * 
		 * 共享一个变量的情况
		 */
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				int i = 10;
				
				//存数据
				map.put(Thread.currentThread(), i);
				
				A a = new A();
				B b = new B();
				a.println();//10
				b.println();//10
			}
		},"线程1").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				int i = 20;
				
				//存数据
				map.put(Thread.currentThread(), i);
				
				A a = new A();
				B b = new B();
				a.println();//20
				b.println();//20
			}
		},"线程2").start();
		
	}
}

public class A {
//public class B {	
	public void println(){
		Thread thread = Thread.currentThread();
		Integer value = Test01.map.get(thread);
		System.out.println(thread.getName() + "中的A类对象调用了println() -- " + value);
    //  System.out.println(thread.getName() + "中的B类对象调用了println() -- " + value);
	}

}

共享多个变量的情况 : 定义数据包类存储

//数据包类
public class Data {
	
	private int i;
	private String str;
	
 
}

public class Test01 {
	
	public static ConcurrentHashMap<Thread, Data> map = new ConcurrentHashMap<>();
	
	public static void main(String[] args) {
		/**
		 * 知识点:线程局部变量的共享
		 * 
		 * 共享多个变量的情况
		 */
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				
				Data data = new Data(10,"aaa");
				
				//存数据
				map.put(Thread.currentThread(), data);
				
				A a = new A();
				B b = new B();
				a.println();//10
				b.println();//10
			}
		},"线程1").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				Data data = new Data(20,"bbb");
				
				//存数据
				map.put(Thread.currentThread(), data);
				
				A a = new A();
				B b = new B();
				a.println();//20
				b.println();//20
			}
		},"线程2").start();
		
	}
}

共享多个变量的情况 - ThreadLocal

public class Test01 {
	
	public static ThreadLocal<Data> local = new ThreadLocal<>();
	
	public static void main(String[] args) {
		/**
		 * 知识点:线程局部变量的共享
		 * 
		 * 共享多个变量的情况 - ThreadLocal
		 */
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				
				Data data = Data.getInstance(10,"aaa");
				
				//存数据
				local.set(data);
				
				A a = new A();
				B b = new B();
				a.println();//10
				b.println();//10
			}
		},"线程1").start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				Data data = Data.getInstance(20,"bbb");
				
				//存数据
				local.set(data);
				
				A a = new A();
				B b = new B();
				a.println();//20
				b.println();//20
			}
		},"线程2").start();
		
	}
}

public class B {
	
	public void println(){
		Thread thread = Thread.currentThread();
		Data value = Test01.local.get();
		System.out.println(thread.getName() + "中的B类对象调用了println() -- " + value);
	}

}

2、需求分析

铁道部发布了一个售票任务,要求销售1000张票,要求有3个窗口来进行销售,
请编写多线程程序来模拟这个效果

public class MyThread extends Thread{
	
	private static int ticket = 1000;
	private static Object obj = new Object();
	
	public MyThread(String name) {
		super(name);
	}

	@Override
	public void run() {
		
		while(ticket > 0){
			//synchronized("abc"){
			//synchronized(Character.class){
			synchronized(obj){
				if(ticket > 0){
					System.out.println(Thread.currentThread().getName() + "正在销售第" + ticket + "张票");
					ticket--;
				}
				if(ticket <= 0){
					System.out.println(Thread.currentThread().getName() + "票已售完");
				}
			}
		}
	}
}


public class Test01 {
	
	public static void main(String[] args) {
		
		MyThread t1 = new MyThread("窗口001");
		MyThread t2 = new MyThread("窗口002");
		MyThread t3 = new MyThread("窗口003");
		
		t1.start();
		t2.start();
		t3.start();
		
		
	}
}

问题1:三个窗口都卖了1000张票,一共卖了3000张
出现原因:三个线程调用三次run方法,就有3*1000张票
解决方案:三个线程共用同一个票的变量

问题2:有些票卖了重票
出现原因:票的输出语句输出后,还没有来得及做票的减减,就被其他线程抢到CPU资源了
解决方案:票的输出语句 和 票的减减必须同时执行完毕后,才能被其他线程抢到CPU资源了 - 加锁

问题3:出现负数
出现原因: 票到了零界点(ticket=1),三个线程都进入循环中
解决方案:锁中再判断一次

锁对象:多个线程要想互斥住,就必须使用同一把锁

synchronized修饰方法

public class MyThread extends Thread{
	
	private static int ticket = 1000;
	
	public MyThread(String name) {
		super(name);
	}

	@Override
	public void run() {
		
		while(ticket > 0){
			method02();
		}
	}
	
	//锁对象:类的字节码文件对象(MyThread.class)
	public static synchronized void method02(){
		if(ticket > 0){
			System.out.println(Thread.currentThread().getName() + "正在销售第" + ticket + "张票");
			ticket--;
		}
		if(ticket <= 0){
			System.out.println(Thread.currentThread().getName() + "票已售完");
		}
	}
	
	//锁对象:this
	public synchronized void method01(){
		if(ticket > 0){
			System.out.println(Thread.currentThread().getName() + "正在销售第" + ticket + "张票");
			ticket--;
		}
		if(ticket <= 0){
			System.out.println(Thread.currentThread().getName() + "票已售完");
		}
	}
}

1.synchronized
同步代码块:
synchronized(锁对象){//自动上锁
…要互斥住的代码…
}//自动解锁
同步方法:
2.Lock

public class MyThread extends Thread{
	
	private static int ticket = 1000;
	private static Lock lock = new ReentrantLock();
	
	public MyThread(String name) {
		super(name);
	}

	@Override
	public void run() {
		
		while(ticket > 0){
			lock.lock();//手动上锁
			if(ticket > 0){
				System.out.println(Thread.currentThread().getName() + "正在销售第" + ticket + "张票");
				ticket--;
			}
			if(ticket <= 0){
				System.out.println(Thread.currentThread().getName() + "票已售完");
			}
			lock.unlock();//手动解锁
		}
	}
}

任务Task : 可以不同线程共用对象

public class Task implements Runnable{

	private int ticket = 1000;
	private static Object obj1 = new Object();
	private Object obj2 = new Object();
	
	@Override
	public void run() {
		
		while(ticket > 0){
//			synchronized("abc"){
//			synchronized(Byte.class){
//			synchronized(obj1){
//			synchronized(obj2){
			synchronized(this){
				if(ticket > 0){
					System.out.println(Thread.currentThread().getName() + "正在销售第" + ticket + "张票");
					ticket--;
				}
				if(ticket <= 0){
					System.out.println(Thread.currentThread().getName() + "票已售完");
				}
			}
			
		}
	}

}

public class Test01 {

	public static void main(String[] args) {
		
		Task task = new Task();
		
		Thread t1 = new Thread(task, "窗口001");
		Thread t2 = new Thread(task, "窗口002");
		Thread t3 = new Thread(task, "窗口003");
		
		t1.start();
		t2.start();
		t3.start();
		
		
	}
}

死锁:锁的嵌套使用可能会导致死锁,应尽量避免

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。首先,广度优先搜索(BFS)是一种图搜索算法,可以用来解决一些问题,比如迷宫问题。在迷宫问题中,我们需要找到一条从起点到终点的最短路径。下面是用 Python 实现 BFS 解决迷宫问题的示例代码: ```python from collections import deque # 定义一个二维数组表示迷宫,0 表示可以走,1 表示障碍物 maze = [ [0, 1, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 1, 0], ] # 定义起点和终点 start = (0, 0) end = (4, 4) # 定义四个方向,上下左右 directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] def bfs(maze, start, end): # 定义队列,初始时将起点加入队列 queue = deque([start]) # 定义 visited 集合,用于记录已经访问过的位置 visited = set([start]) while queue: # 取出队列中的第一个节点 node = queue.popleft() # 如果当前节点是终点,返回到达终点的最短距离 if node == end: return distance[node[0]][node[1]] # 对于当前节点,遍历它的四个方向 for direction in directions: # 计算出下一个节点的坐标 next_node = (node[0] + direction[0], node[1] + direction[1]) # 如果下一个节点不越界且没有访问过,并且可以走(maze[next_node[0]][next_node[1]] == 0),将它加入队列和 visited 集合 if 0 <= next_node[0] < len(maze) and 0 <= next_node[1] < len(maze[0]) and next_node not in visited and maze[next_node[0]][next_node[1]] == 0: queue.append(next_node) visited.add(next_node) # 如果没有找到到达终点的路径,返回 -1 return -1 # 计算每个位置到起点的最短距离 distance = [[float('inf') for _ in range(len(maze[0]))] for _ in range(len(maze))] distance[start[0]][start[1]] = 0 bfs(maze, start, end) ``` 在上面的代码中,我们首先定义了一个迷宫,然后定义了起点和终点。接着,我们定义了四个方向,上下左右。接下来,我们定义了 bfs 函数,用于实现广度优先搜索。在 bfs 函数中,我们首先定义了一个队列和 visited 集合,用于记录已经访问过的位置。然后,我们将起点加入队列和 visited 集合。接着,我们进行循环,取出队列中的第一个节点,遍历它的四个方向。如果下一个节点不越界且没有访问过,并且可以走,我们将它加入队列和 visited 集合。最后,如果没有找到到达终点的路径,返回 -1。 最后,我们计算每个位置到起点的最短距离,并调用 bfs 函数求解最短路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值