老师,在自己写线程代码时,不知道怎么确定共享资源,如果自己确定这个例子的共享资源,不知到怎么下手。有没有什么好的办法确定共享资源?谢谢
老师,是如何保证发送线程和接受线程的锁都是同一个锁呢,视频中的是”abc“,那如果一个是”abc“,一个是”abcd“还能保证这两个线程是同一个锁吗
报错,请老师指点迷津。
hashset.rar
老师,使用 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); } }
老师,我的复制系统回应,没有办法访问,怎么办?
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(); } } } }}
老师,这里是按照key进行比较排序,如果想比较值怎么比较(在treemap中比较Integer类型的数值)
老师我的这个hashmap添加不了值 ,显示空指针异常,不知道怎么办了,老师帮忙给看看
源码
源码.zip
HashMap 与 Hashtable 的区别
JDK引入版本
请问下做出来那个电话本项目后打开笔记本里面是乱码怎么解决啊
chatRoom.zip
老师,这个问题找了半天也没找出为什么,服务器能接受消息,但是客户端收不到服务端的消息。
这部分的源码分析,应该是说的这个节点的头地址和尾地址以及中间的值,老师讲的一会这个指向这个,我反正觉得听不懂
bw.write(line); bw.newLine(); bw.flush();
问题:1.为什么要newLine()换行,我测试加与不加没区别
2.每次循环都要flush()刷新缓冲区,如果放在循环外面可以吗?高层流close关闭的情况下不加flush是否可以,还是说操作规范来讲最好在循环中都加上flush?
为什么是readLong?难道是因为读的多吗?还是因为基本类型是long?为什么不能是int?
一个类中存在多个构造器如何实现多个构造器的实例化
/** * 放馒头 */ public synchronized void push(ManTou manTou){ while(this.index == this.mt.length){ try{ this.wait(); }catch (InterruptedException e){ e.printStackTrace(); } } this.notify(); this.mt[this.index] = manTou; this.index++; } /** * 取馒头 */ public synchronized ManTou pop(){ while(this.index == 0){ try { this.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } this.notify(); this.index--; return this.mt[this.index]; }
老师,我想问下执行完notify之后,是把当前线程的对象锁移交给在就绪状态使用相同对象锁的另一个线程吗?那么当前线程中的有关对象里的属性还能继续使用么,比如push方法在执行notify()后,SyncStack类的对象ss中的mt数组和index变量还有值么?唤醒之后push方法后面的代码还继续执行吗?还是直接到pop()中执行?为什么?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637