会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132464个问题
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 35207楼
WEB前端全系列/第一阶段:HTML5+CSS3模块/浮动与定位 35208楼
Python 全系列/第一阶段:Python入门/控制语句 35210楼
Python 全系列/第二阶段:Python 深入与提高/文件处理 35211楼
Python 全系列/第二十阶段:数据分析-统计分析/假设检验 35213楼

while(true){ String msg=br.readLine(); synchronized("abc"){ //把读取到的数据写入公共数据区 ChatRoomServer.buf="["+this.socket.getInetAddress()+"]:"+msg; //唤醒发送消息的线程对象。 "abc".notifyAll(); } } while(true){ synchronized("abc"){ //把读取到的数据写入公共数据区 ChatRoomServer.buf="["+this.socket.getInetAddress()+"]:"+br.readLine(); //唤醒发送消息的线程对象。 "abc".notifyAll(); } }分析一下在实现一对多交流的过程中为什么前面的代码可以实现功能,而后面那段代码会进入阻塞class ChatSend extends Thread{
    private Socket socket;
    /**创建发送数据线程*/
    /**
     * 向客户端发送消息的线程类
     */
    public ChatSend(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        this.sendMsg();
    }

    /**
     * 将公共数据区的消息发送给客户端
     */
    public void sendMsg(){
        try(PrintWriter pw=new PrintWriter(socket.getOutputStream());){
                while(true){
                    synchronized ("abc"){
                        "abc".wait();
                        pw.println(ChatRoomServer.buf);
                        pw.flush();
                    }
                }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
/**
 * 接收客户端消息的线程类
 */
class ChatReceive extends Thread{
    private Socket socket;
    public ChatReceive(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
            this.receiveMsg();
    }

    /**
     * 实现接收客户端发送的消息
     */
    public void receiveMsg(){
        try(BufferedReader br=new BufferedReader(new InputStreamReader(this.socket.getInputStream()))){
            while(true){
                String msg=br.readLine();
                synchronized("abc"){
                    //把读取到的数据写入公共数据区
                ChatRoomServer.buf="["+this.socket.getInetAddress()+"]:"+msg;
                    //唤醒发送消息的线程对象。
                "abc".notifyAll();
            }
            }

        }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());
                new ChatReceive(socket).start();
                new ChatSend(socket).start();

            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

老师,为什么我一开始用的后面那段代码运行后是陷入阻塞的,用了课上老师的前面那段代码就好了,我个人感觉br.readLine()放在哪里好像没什么影响,就不是很理解为什么会陷入阻塞

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程 35216楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        .box{
            width: 800px;
            height: 50px;
            background-image: linear-gradient(20deg,rgb(69, 120, 221),rgb(44, 172, 222),rgb(29, 203, 177));
            border-radius: 30px;
            margin: 0 auto;
            
        }
        
        ul{
            list-style: none;
            display: flex;    /*父元素设置为弹性容器,默认为横向排列*/
            justify-content: center;
            line-height: 50px; /*设置上下居中*/
            
        }


        ul a{
            color: rgb(173, 10, 232);
            text-decoration: none; /*去除下划线*/
            margin:0 10px;         
            font-size: 20px;
            font-weight: bolder;

        }

        a:visited{
            color: rgb(173, 10, 232);
        }

        a:hover{
            color: rgb(43, 226, 192);
        }

        a:active{
            color: hotpink;
            font-size: 26px;
        }
    </style>
</head>
<body>
    <div class="box">
        <ul>
            <li><a href="#">生活</li>
            <li><a href="#">萌宠</li>
            <li><a href="#">游戏</li>
            <li><a href="#">动漫</li>
            <li><a href="#">电视剧</li>
        </ul>
    </div>
</body>
</html>

老师我这样已经实现了内容上下左右居中对齐,并且整齐排列的导航栏效果。但是当我去掉line-height属性,将其变为盒子模型属性align-items想让他直接竖直居中对齐,结果失败了。为什么?我改过ul a{}里面的margin属性去掉了上下为0的设置,结果还是实现不了垂直方向居中。

WEB前端全系列/第一阶段:HTML5+CSS3模块/CSS盒子模型 35218楼
JAVA 全系列/第四阶段:网页编程和设计/CSS3(旧) 35220楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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