会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132838个问题

Desktop.rar

    image.png    image.png

为什么添加元素只添加第一个、第二个、和最后一个啊?

是哪里的问题啊?

JAVA 全系列/第二阶段:JAVA 基础深化和提高/数据结构 1951楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 1953楼

老师 我有点不明白 为什么输入http://localhost:8080/ 之后就会弹出这个界面呢?这其中的原理是什么?image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 1954楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 1955楼

package com.Jin.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Test {
    public static void main(String[] args) throws IOException {
        System.out.println("__________服务器已启动______________");
        //(1)创建ServerSocket对象
        ServerSocket server = new ServerSocket(9999);
        //(2)监听是否有客户端来请求连接
        Socket client = server.accept();
        //(3)获取输入流
        InputStream is = client.getInputStream();
        System.out.println((char)is.read());
        //(4)获取输出流
        //“收到了”
        OutputStream os = client.getOutputStream();
        os.write("收到了!".getBytes());
        //(5)关闭流,关闭Socket
        if (os!=null){
            os.close();
        }
        if (is!=null){
            is.close();
        }
        if (client!=null){
            client.close();
        }
    }
}



package com.Jin.client;

import com.sun.org.apache.xpath.internal.operations.String;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Test {
    public static void main(String[] args) throws IOException {
        //(1)创建Socket对象
        Socket client = new Socket("192.168.0.101",9999);
        //(2)获取输出流
        OutputStream os = client.getOutputStream();
        os.write('a');
        //(3)获取输入流
        InputStream is = client.getInputStream();
        byte [] buf = new byte[1024];//中转站
        int len = 0;//读到字节的个数
        while ((len=is.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }
        //(4)关闭流
        if (is!=null){
            is.close();
        }
        if (os!=null){
            os.close();
        }
        if (client!=null){
            client.close();
        }

    }

}

启动不了,显示错误

image.png


image.png






JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 1958楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1959楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器 1960楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 1961楼

老师,同步代码块中 wait()方法及 notify() 方法是用 this 对象调用的,那这个对象到底是哪个类下的对象?是 缓冲区 BufferZone 类下实例化的对象嘛?

// 馒头类
class SteamBread {
    private int id;

    public SteamBread(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }
}

// 缓冲区类
class BufferZone {
    // 存放馒头的盒子
    private SteamBread[] sb = new SteamBread[10];
    // 定义操作盒子的索引
    private int index;

    // 生产者方法馒头
    public synchronized void put(SteamBread steamBread) {
        while (this.index >= this.sb.length) {
            try {
                this.wait();   // this 引用当前类的实例,所以对应的对象为 BufferZone 类下实例化的对象 bz
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.sb[this.index] = steamBread;
        this.index++;
    }

    // 取馒头
    public synchronized SteamBread take() {
        while (this.index == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.notify();
        this.index--;
        return this.sb[this.index];
    }
}

// 生产者线程类
class ProducerThread extends Thread {
    private BufferZone bz;

    public ProducerThread(BufferZone bz) {
        this.bz = bz;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; ++i) {
            System.out.println("生产馒头:" + i);
            SteamBread sb = new SteamBread(i);
            this.bz.put(sb);
        }
    }
}

// 消费者线程类
class ConsumerThread extends Thread {
    private BufferZone bz;

    public ConsumerThread(BufferZone bz) {
        this.bz = bz;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; ++i) {
            SteamBread sb = this.bz.take();
            System.out.println("消费馒头:" + i);
        }
    }
}


// 测试生产者消费者模式
public class ProducerConsumerModel {
    public static void main(String[] args) {
        BufferZone bz = new BufferZone();
        new ProducerThread(bz).start();
        new ConsumerThread(bz).start();
    }

}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 1965楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637