会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132837个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2597楼

//编写一个赋值的方法 同步监视器为Goods类的对象
public synchronized void set(String name,String brand){
    if (isFlag){//相当于isFlag==true
        try {
            super.wait();       //生产者线程等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    this.setName(name);
    try {
        Thread.sleep(300);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    this.setBrand(brand);
    System.out.println("========生产者线程产生了============="+this.getBrand()+"========="+this.getName());
    //通知消费者
    super.notify();
    isFlag=true;
}
//编写一个取值的方法
public synchronized  void get(){
    if (!isFlag){
        try {
            super.wait();       //消费者等待
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    System.out.println("消费者线程取走了-------"+this.getBrand()+"----------"+this.getName());

    super.notify();//通知生产者线程
    isFlag=false;//没有商品了
}

老师我想问一下,就是比如说isFlag的值为true的话,他使用wait()方法进行线程等待,那么程序是直接跳到下面的get方法中去,等get()方法执行完毕之后,在返回去执行

.setName(name){
    Thread.()} (InterruptedException e) {
    e.printStackTrace()}
.setBrand(brand)System..println(+.getBrand()++.getName()).notify()=

这部分么。

他的这个线程等待就是等另外的一个线程执行完之后,在进行自己本身的代码嘛

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 2598楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程和并发编程(旧) 2602楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 2603楼

image.png

HandServer0.5.zip



老师,您好,一直出现空指针异常,找了许久也没找到问题,麻烦老师帮忙看一下

JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 2604楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 2607楼

package com.bjsxt.client;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
 
/**
 * 客户端(做好准备与服务器端通信),以字节流作为传输方式
 * @author Administrator
 *
 */
public class TestClient {
    public static void main(String[] args) throws UnknownHostException, IOException {
        //新建socket对象
        Socket client = new Socket("127.0.0.1",9999);
         
        //新建输出流对象(因为要给服务器端发送消息,所以得有输出流对象)
        OutputStream os = client.getOutputStream();
    //  os.write("来自客户端的请求:abc".getBytes());
        os.write("来自服务器的请求:abc".getBytes());
         
        //打印一句话表明,客户端已发出请求
        System.out.println("---客户端已发出请求---");
         
        //新建输入流对象(从服务器端接收消息,所以得有输入流对象,相当于从服务器端读取内容)
        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));
        }
         
        //关闭流
        if(is!=null) {
            is.close();
        }
        if(os!=null) {
            os.close();
        }
    }
 
}

Error:(19, 32) java: 无法将类 com.sun.org.apache.xpath.internal.operations.String中的构造器 String应用到给定类型;

  需要: 没有参数

  找到: byte[],int,int

  原因: 实际参数列表和形式参数列表长度不同

老师这是什么原因呀

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

已经导入jaxen包了,为啥还是会出现找不包的异常,用的是IDEA

package com.zheng.TestXML;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.io.File;

public class TextXpath {
    public static void main(String[] args) throws DocumentException {
        //1) 创建 SAXReader 对象
        SAXReader reader = new SAXReader();
        //2) 调用 read 方法
        Document doc = reader.read(new File("books.xml"));
        //3)
        Node node = doc.selectSingleNode("//name");
        System.out.println("节点的名称:"+node.getName()+"节点的值"+node.getText());
    }
}

"E:\学习软件大集合\IDEA\IntelliJ IDEA 2019.3.2\jbr\bin\java.exe" "-javaagent:E:\学习软件大集合\IDEA\IntelliJ IDEA 2019.3.2\lib\idea_rt.jar=49244:E:\学习软件大集合\IDEA\IntelliJ IDEA 2019.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\zheng\Desktop\mycode\Process\out\production\XML;C:\Users\zheng\Desktop\mycode\Process\lib\jdom.jar;C:\Users\zheng\Desktop\mycode\Process\lib\dom4j-1.6.1.jar com.zheng.TestXML.TextXpath
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.dom4j.io.SAXContentHandler (file:/C:/Users/zheng/Desktop/mycode/Process/lib/dom4j-1.6.1.jar) to method com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser$LocatorProxy.getEncoding()
WARNING: Please consider reporting this to the maintainers of org.dom4j.io.SAXContentHandler
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
	at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
	at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
	at org.dom4j.tree.AbstractNode.selectSingleNode(AbstractNode.java:183)
	at com.zheng.TestXML.TextXpath.main(TextXpath.java:22)
Caused by: java.lang.ClassNotFoundException: org.jaxen.JaxenException
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
	... 4 more


JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2609楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 2610楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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