m.put(e, PRESENT) == null 这里有个判空,那么
map的put()方法什么时候返回null呢
oldLu这里没有讲啊
我试着解答一下楼上同学的问题:
大用1,小用-1
相等用啥,是看name判断的结果
我看到最后这里在size++后边还有个modCount++,
这个modCount是什么,成员变量里边似乎没有它。
我这里有个疑问。
如图所示,pop()和push(E e)都有等效的方法,那这两个方法设计出来有什么意义呢
ArrayList源码好像没有讲
/** * 创建缓冲区 * 要点: * 1、创建缓冲区空间大小(目前是数组空间) → 生产者生产数据 → 判断空间是否满了?满了就进入阻滞状态调用Object下面wait方法 * 满了消费者没来拿数据,就需要唤醒消费者 (调用Object下面notify方法唤醒单个线程) * 2、消费者来拿 →判断有没有数据 → 如果没了就进入阻滞状态 → 同时唤醒生产者线程 */ //定义馒头类 class ManTou{ private int id; public ManTou(int id){ this.id=id; } public int getId() { //私有属性需要 return this.id; } } //定义缓冲区 class SycStack{ //缓冲区不需要继承谁,它只是一个放数据地方 //定义放馒头的空间 private ManTou[] mt =new ManTou[10]; //定义操作空间的索引; private int index; //放馒头 public synchronized void push(ManTou manTou) throws InterruptedException { while(this.index==this.mt.length){ //放数据之前要先判断空间满没满,用循环多判断几次,一次不保险 this.wait(); //Object 类下的 让线程休眠的方法; 必须在synchronized域内使用。 会先释放对象锁,再休眠。 } this.notify(); //Object 类下 唤醒单个线程的方法,必须在synchronized域内使用。会唤醒在等待队列状态中的 单个线程。/notifyAll是全部线程 this.mt[this.index]=manTou; //放馒头时需要知道位置,默认是0; this.index++; //每放一个,索引+1,直到空间满了 } //取馒头 public synchronized ManTou pop() throws InterruptedException { while (this.index==0){ this.wait(); } this.notify(); //对方线程若是处于运行状态 唤醒也无妨 this.index--; return this.mt[this.index]; //先判断,不为空则 拿一个,拿完之后把剩下的返回; } } //定义生产者线程 class ShengChan extends Thread{ private SycStack ss; //生产的数据要放在缓冲区, public ShengChan(SycStack ss){ this.ss=ss; } @Override public void run() { for(int i=0;i<10;i++){ System.out.println("生产的馒头"+i); ManTou mt=new ManTou(i); //不知道这里为什么这么做,要创建馒头对象 try { this.ss.push(mt); //调用放馒头的方法 } catch (InterruptedException e) { e.printStackTrace(); } } } } //定义消费者线程 class XiaoFei extends Thread{ private SycStack ss; //消费的数据也放在缓冲区, public XiaoFei(SycStack ss) { this.ss = ss; } @Override public void run() { for(int i=0;i<10;i++){ try { ManTou manTou = this.ss.pop(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("消费的馒头"+i); } } } public class TestProduceTread { public static void main(String[] args) { //s实例化缓冲区 SycStack ss=new SycStack(); //实例放、取馒头化线程 new ShengChan(ss).start(); new XiaoFei(ss).start(); } }
老师为什么我的代码结果是顺序排的?试了几次都这样
线程安全和线程同步是不是一个意思
static method不能使用类上定义的泛型,能说下具体原因吗?
所有代码写完测试出异常求老师解惑
/** * 接受客户端消息的线程类 */ class ChatReceive implements Runnable{ private Socket socket; public ChatReceive(Socket socket) { this.socket = socket; } @Override public void run() { this.receiveMsg(); } /** * 实现接收客户端发送的消息 */ private void receiveMsg(){ try(BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()))){ while (true){ String msg = br.readLine(); synchronized ("abc"){ //将读取到的数据写入到公共数据区 ChatRoomServer.buf = "[" + this.socket.getInetAddress() + "]: " + msg; } //唤醒发送消息的线程对象 "abc".notifyAll(); } }catch (Exception e){ e.printStackTrace(); } } } /** * 向客户端发送消息的线程类 */ class ChatSend implements Runnable{ private Socket socket; public ChatSend(Socket socket) { this.socket = socket; } @Override public void run() { this.sendMsg(); } /** * 将公共数据区的消息发送给客户端 */ private void sendMsg(){ try (PrintWriter pw = new PrintWriter(this.socket.getOutputStream());){ while (true){ synchronized ("abc"){ //让发消息的线程处于等待状态 "abc".wait(); //将公共数据区中的消息发送给客户端 pw.println(ChatRoomServer.buf); pw.flush(); } } }catch(Exception e){ e.printStackTrace(); } } } public class ChatRoomServer { //定义公共数据区 public static String buf; public static void main(String[] args) { System.out.println("Chat Server Version 1.0"); System.out.println("Listen at 8888....."); try(ServerSocket serverSocket = new ServerSocket(8888)){ while (true){ Socket socket = serverSocket.accept(); System.out.println("连接到:" + socket.getInetAddress()); Thread t1 = new Thread(new ChatReceive(socket)); Thread t2 = new Thread(new ChatSend(socket)); t1.start(); t2.start(); } }catch (Exception e){ e.printStackTrace(); } } }
连接成功后:客户端一直循环输出他说:null
服务端报异常
要是key键所对应value被删除后key键也会消失吗
右边的文档里的代码正确吗,为什么运行之后还是和之前一样,synchronized的功能没有显示出来
package com.itbaizhan; class Iphone{ private String name; public Iphone(String name){ this.name=name; } public void show(){ System.out.println("我是 "+name+"可以在屏幕上显示"); } } class touyingPhone{ public Iphone iphone; //它只是个扩展,必须有前一个东西才能扩展。这里的Iphone,就是继承于Iphone的意思。iPhone是参数 public touyingPhone(Iphone iphone){ this.iphone=iphone; } //功能更强的 方法 public void show(){ iphone.show(); System.out.println("还能投影,在墙上"); } } public class TestDecoration { public static void main(String[] args) { Iphone i=new Iphone("苹果199代"); i.show(); System.out.println("======加上装饰器后======"); touyingPhone typ=new touyingPhone(Iphone); typ.show(); } }
这个报错实在是找不到该怎么解决,视频看了两遍了都没头绪
不是比较完大用1表示,小用-1,相等用0 吗,这里相等的情况为什么突然又调用compareTo啊
老师这里判断头结点和尾结点 可不可以这样写 node == this.head node == this.tail
老师这里的泛型为什么要用Integer不用int呢
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637