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

老师,同步代码块中 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 基础深化和提高/多线程技术(旧) 1967楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 1970楼

老师,使用 new 关键字新建 Student 类的对象 student,和获取 Class 对象,然后调用 newInstance() 方法新建对象 students 有啥区别?应该都算是调用 Student 类的无参构造器新建对象吧。

public class ReflectionPerformanceTest01 {

    public static void main(String[] args) throws Exception {
        // Reflection
        // obtain Class object
        Student student = new Student();
        Class classObject01 = student.getClass();

        Class classObject02 = Student.class;

        Class classObject03 = Class.forName("com.bjsxt.demo.Student");

        // test students is equal student?是不是都是算是调用无参构造器创建对象?
        // 此外,使用这两种方式创建的对象调用方法,很明显一个使用了反射,一个没有使用反射,所以效率会有明显的差距。
        Student students =(Student) classObject03.newInstance();

        long reflectionStart = System.currentTimeMillis();
        Method method = classObject03.getMethod("setName", String.class);

        final int cycle = 10000000;
        for ( int i = 0; i < cycle; ++i) {
            // method.invoke(student,"John");   // new 关键字新建对象 student
            method.invoke(students,"John");   // Class 对象调用 newInstance() 方法获取对象
        }
        long reflectionEnd = System.currentTimeMillis();
        System.out.println(reflectionEnd - reflectionStart);
        System.out.println("----------------");

        // non reflection
        long start = System.currentTimeMillis();
        for (int i = 0; i < cycle; ++i) {
            student.setName("Marry");
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 1971楼

老师,我的复制系统回应,没有办法访问,怎么办?

package IO小练习;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test文件夹的复制 {
 public static void main(String[] args) {
  Test文件夹的复制 ui=new Test文件夹的复制();
  File op=new File("D:\\test.txt");
  File ip=new File("D:\\test04.txt");
  ui.copy(op,ip);
  File lp=new File("D:\\zzx");
  File ij=new File("D:\\zzx02");
  ui.copy(lp, ij);
 }
 //复制所有目录下的文件
 public void copydir(File src,File url)
 {
  if(!url.exists())//如果文件夹不存在,首先要进行创建文件夹。
  {
   url.mkdir();
  }
  File[] op=src.listFiles();//获取指定目录下所有的文件。
  for (File file : op) {
   if(file.isFile())//如果是目录那么就复制
   {
    copy(new File(src+"\\"+file.getName()),new File(url+"\\"+file.getName()));
   }
   else
   {
    copydir(new File(src+"\\"+file.getName()),new File(url+"\\"+file.getName()));
   }
  }
 }
 //复制一个目录下的文件
  public void copy(File src,File url) {
   BufferedInputStream io=null;
   BufferedOutputStream up=null;
   try {
   io=new BufferedInputStream(new FileInputStream(src));
     up=new BufferedOutputStream(new FileOutputStream(url));
    byte[] i= new byte[1024];
    int len=0;
    while((len=io.read(i))!=-1) {
     System.out.println(new String(i,0,len));
     up.write(i);
    }
 } catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }finally
   {
  if(up!=null) {
   try {
    up.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    io.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
   }
  
  }
}

2019-07-07_142354.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1972楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 1973楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 1974楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 1976楼

chatRoom.zip

老师,这个问题找了半天也没找出为什么,服务器能接受消息,但是客户端收不到服务端的消息。

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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