MyDoublyLinkedList <>MyList<>{ Node<>{ Node<> Node<> (itemNode<> prevNode<> next) { .= item.= prev.= next} } Node Node (element) { .LinkLast(element)} (element){ Node t = .Node<> node = Node<>(elementt).= node(t==){ .= node}{ t.= node} .++} (index) { .checkIndex(index)Node<> node = .getNode(index)node.} (index){ (index<||index>.-){ IndexOutOfBoundsException()} } Node (index){ (index<(.>>)){ Node<> a = .(i=i<=indexi++){ a = a.} a}{ Node<> a = .(i=.-i>=indexi--){ a = a.} a} } () { .} (index) { .checkIndex(index)Node<> node = getNode(index)item = node.(index==){ .= node.node..= }(index==.-){ .= node.node..= }{ Node<> node1 = getNode(index-)Node<> node2 = getNode(index+)node1.= node2node2.= node1} .--item} (element){ .LinkFirst(element)} (element){ Node<> a = .Node<> node = Node<>(elementa).= node(a==){ .=node}{ a.= node} .++} (element){ .LinkLast(element)} (String[] args) { MyDoublyLinkedList<String> myList = MyDoublyLinkedList<>()myList.add()System..println(myList.get())System..println(myList.size())} }
客户端:
package com.bjsxt; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class BasicSocketServer { public static void main(String[] args) { Socket socket = null; BufferedReader br = null; try { ServerSocket serverSocket = new ServerSocket(8888); System.out.println("服务器启动监听,等待链接。。。。"); //开启端口的监听 socket = serverSocket.accept(); //读取客户端发送的消息 br = new BufferedReader(new InputStreamReader(socket.getInputStream())); System.out.println(br.readLine()); }catch (IOException e) { e.printStackTrace(); } finally { if (br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
服务器:
import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; public class BasicSocketClient { public static void main(String[] args) { Socket socket = null; PrintWriter pw = null; //创建socket对象:两个参数:1.服务端的ip地址 2.服务端监听的端口 try { socket = new Socket("127.0.0.1",8888); pw = new PrintWriter(socket.getOutputStream()); pw.println("服务端你好"); pw.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if (pw!=null){ pw.close(); } if (socket!=null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
为何无输出
老师,运行结果的最上面一行结果是什么意思?
之前的那个女老师讲的啥玩意,一个天上一个地下,浪费好多精力
不太明白为什么放馒头和去馒头要线程同步,之前说解决的忙闲不均的问题不就代表他俩互不干扰吗,还是说要保证缓冲区有东西什么的?
视频2:00 处,有点不懂,
按照老师的意思,这里的异常不能在run()方法中抛出去的吗?
这是为什么呢?
还是说我理解错了,这里也科通通过throw在run方法处抛出异常?
com.bjsxt; Programmer{ String ; Programmer(String name){ .=name; } Computer(){ () { { System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); } (InterruptedException e) { e.printStackTrace(); } } } Coding(){ () { { System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); } (InterruptedException e) { e.printStackTrace(); } } } toilet(){ () { { System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); System..println(.+ ); Thread.(); } (InterruptedException e) { e.printStackTrace(); } } } } Working1 Thread{ Programmer ; Working1(Programmer p){ .=p; } run() { ..Computer(); } } Working2 Thread{ Programmer ; Working2(Programmer p){ .=p; } run() { ..Coding(); } } Toilet Thread{ Programmer ; Toilet(Programmer p){ .=p; } run() { ..toilet(); } } TestSyncThread { main(String[] args) { Programmer p = Programmer(); Programmer p1 = Programmer(); Programmer p2 = Programmer(); Toilet(p).start(); Toilet(p1).start(); Toilet(p2).start(); } }
输出:
JY 打开卫生间门
JY 上厕所
JY 冲水
JY 离开卫生间
xj 打开卫生间门
xj 上厕所
xj 冲水
xj 离开卫生间
SJ 打开卫生间门
SJ 上厕所
SJ 冲水
SJ 离开卫生间
这里先输出xj是为什么,还是顺序就是随机的?
//为什么关闭流的时候是判断!=null,既然是等流执行完毕后关闭,那意思肯定是当流中没有数据时关闭流,应该是==null啊,难道是为了防止空指针
老师,这节中在关闭字节数组输入流为什么,不进行
if(bis!=null)
的判断操作,而是直接关闭
老师,为什么我这个代码好奇怪,为什么我都是先生产完再消费?
一、
/** * 对象流将list存储(序列化)到文件中 */ public void savePersonList() { ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/a.txt"))); for (int i = 0; i < this.list.size(); i++) { oos.writeObject(this.list.get(i)); } oos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (oos != null) { oos.close(); } } catch (Exception e) { e.printStackTrace(); } } }
二、
/** * 获取文件的对象信息(对象的反序列化) */ public void getPersonList() { //声明流 FileInputStream fis = null; ObjectInputStream ois = null; try{ //实例化流对象 fis = new FileInputStream("d:/a.txt"); ois = new ObjectInputStream(new BufferedInputStream(fis)); try{ while (fis.available()==0){ Person person = (Person) ois.readObject(); list.add(person); } }catch (Exception e){ System.out.println("获取上次记录成功!"); } }catch (Exception e){ e.printStackTrace(); }finally { try{ if (fis!=null){ fis.close(); } if (ois!=null){ ois.close(); } }catch (Exception E){ E.printStackTrace(); } } }
三、问题(程序可以运行成功。实现对象存入文件,并启动程序时读取文件中的对象到list)
遇到的问题:1、代码块二中,while循环里判断条件是(fis.available()==0),我是在网上找的这个判断读取完成的条件。如果条件换成true也可以运行出来,老师可以讲讲区别吗,available方法是什么作用?
2、代码块二中、这里流的关闭顺序不清楚(主要是开启的顺序不知道)
3、老师看看代码有什么需要改进的地方吗?
老师,在启动子线程后再让线程进入阻塞状态,之后终止线程。
那让线程进入阻塞状态只能用System.in.read();吗?
synchronized里面是(Object.class)吗?如果可以,意思是不是就是实例化的是所有对象都有互斥?
if (fis !=null);
try{ fis.close};
请问为啥是在!=null的情况下关闭流对象啊
老师出现这个编译错误是怎么回事
public class Test1 { private int value; private static Test1[] cache; static{//[-100,100] for(int i = -100;i<=100;i++){ cache[i+100] = Test1.valueOf(i); } } public static Test1 valueOf(int i){ if(i>=-100&&i<=100){ return cache[i+100]; } return new Test1(i); } private Test1(int i){ this.value = i; } public static void main(String[] args) { Test1 t1 = Test1.valueOf(1); System.out.println(t1); } }
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637