package com.bjsxt.ObjectLiu; import java.io.*; public class TestStudent { public static void main(String[] args) { //write();//调用写对象的方法 read();//调用读对象的方法 } public static void write(){ ObjectOutputStream oos= null; try { //oos = null; oos=new ObjectOutputStream(new FileOutputStream("E:\\student.txt")); Student stu=new Student("marry",20,"888888"); Student.schoolName="JN校区"; oos.writeObject(stu); } catch (IOException e) { e.printStackTrace(); }finally { //关闭 if (oos!=null){ try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void read(){ ObjectInputStream ois= null; try { ois = null; ois=new ObjectInputStream(new FileInputStream("E:\\student.txt")); Student stu = (Student) ois.readObject(); System.out.println(stu); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { //关闭流 if(ois!=null){ try { ois.close(); } catch (IOException e) { e.printStackTrace(); } } } } } //打印结果:Student{name='marry', age=20, pwd='null'}schoolName=null //杜老师,为什么我的打印结果含有单引号,而讲课的老师的打印结果是没有单引号的,麻烦您帮忙看一下,谢谢您
InetSocketAddress isa = new InetSocketAddress("localhost",9999); InetSocketAddress isa2 = new InetSocketAddress("127.0.0.1",9999); InetSocketAddress isa3 = new InetSocketAddress("192.168.1.2",9999);
isa返回结果:
isa2返回结果:
isa返回结果:
老师这三个不都是指本机嘛,为什么三次返回的主机名称不一样?只有输入IP地址的返回的我电脑的名称?
我这个问题是在哪?
/** * 账户类 */ class Account{ private String accountNum;//账户 private double balance;//余额 public Account(){ } public Account(String accountNum, double balance) { this.accountNum = accountNum; this.balance = balance; } public String getAccountNum() { return accountNum; } public void setAccountNum(String accountNum) { this.accountNum = accountNum; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } } class DrawMoney extends Thread{ private Account account;//账户对象 private double drawMoney;//取款金额 public DrawMoney(String name,Account account,double drawMoney){ super(name); this.account=account; this.drawMoney=drawMoney; } /** * 取款线程 */ @Override public void run() { if (this.account.getBalance()>=this.drawMoney){ System.out.println(this.getName()+"取钱成功 "+this.drawMoney); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } this.account.setBalance(this.account.getBalance()-this.drawMoney); System.out.println("\t 余额为:"+this.account.getBalance()); }else { System.out.println(this.getName()+"取钱失败"); } } } public class DrawMoneyThread { public static void main(String[] args) { Account account=new Account("1234",1000); new DrawMoney("小王",account,800).start(); new DrawMoney("李梅",account,500).start(); } }
老师,我把程序锁写在这也可以实现程序互斥,请问这样是可以的吗
如果String输入的是汉字,它的排序规则是什么?
怎么换行的老师,控制栏,我一按回车就是执行
老师,通过构造方法对象调用newInstance()创建对象,这个newInstance()不是过期了吗?
为什么我这个ab.txt会把同磁盘同目录下的AB,txt覆盖
原来的AB.txt里写的是qwertyuiop,难道不区分大小写的吗
老师我发现视频里的复制目录方法有个小问题,如果targetDir是srcDir目录下的一个文件,那执行方法的时候就会陷入无限循环,不断的创建文件,像下面这种情况:
public static void main(String[] args) { File srcFile = new File("E:\\百战学习笔记\\002第二周学习\\06-IO流技术"); File targetFile = new File("E:\\百战学习笔记\\002第二周学习\\06-IO流技术\\复制练习"); //复制文件 // copyFile(srcFile, targetFile); //复制指定目录下的所有文件和文件夹下的子文件 copyDir(srcFile, targetFile); }
然后执行的时候就不断循环:
这个问题可以怎样解决呢
package cn.sxt.thread2; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 测试练习Lock,实现同步的第三种方式 * @author User * */ public class TestLock { public static void main(String[] args) { /* Test2 t2=new Test2(); new Thread(t2,"A").start(); new Thread(t2,"B").start(); new Thread(t2,"C").start(); */ Test3 t3=new Test3(); FutureTask ft=new FutureTask(t3); new Thread(ft,"A").start(); new Thread(ft,"B").start(); new Thread(ft,"C").start(); } } class Test2 implements Runnable{ private int count=0;//默认数 Lock lock=new ReentrantLock(); //创建锁对象 @Override public void run() { for(int a=0;a<10;a++) { try { lock.lock();//上锁 count++; // TODO Auto-generated method stub try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"执行操作:count="+count); }finally { lock.unlock(); //解锁 } } } } class Test3 implements Callable{ private int count=0; Lock lock=new ReentrantLock();//创建锁对象 @Override public Object call() throws Exception { for(int a=0;a<10;a++) { try { lock.lock(); count++; // TODO Auto-generated method stub try { Thread.sleep(300); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+"执行操作:count="+count); }finally { lock.unlock(); } } return count; } }
为什么实现Callable 接口使用同步lock的结果和实现Runnablle接口使用同步锁的结果完全不一样?实现Callable接口,得到的结果要么只打印A执行操作,要么只打印B执行操作? 看了几遍课程,老师也没讲到这个。
请问,老师,String str = br.readLine();在读不到数据的时候会卡着不会动。那么红框的地方,当文件被读取完毕,没有数据时,应该也会卡着不动才对,为什么这里可以执行完程序。
老师,赋值不是等号右边赋给左边吗,左边也可以赋给右边吗
这里哪错了
老师我的输出结果为什么会跟老师出现差异?
老师 我还没明白这里的while 和 if 有什么区别,当使用if的时候,如果条件成立,那么线程也会进入一个等待状态,下面的代码不会被执行,知道被唤醒 ,唤醒之后是从while再次判断呢?还是直接就执行下面的代码,不会再经过while判断了?感觉if 和 while的效果应该是一样的吧?
非常抱歉给您带来不好的体验!为了更深入的了解您的学习情况以及遇到的问题,您可以直接拨打投诉热线:
我们将在第一时间处理好您的问题!
关于
课程分类
百战程序员微信公众号
百战程序员微信小程序
©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园网站维护:百战汇智(北京)科技有限公司 京公网安备 11011402011233号 京ICP备18060230号-3 营业执照 经营许可证:京B2-20212637