2020.02-Study_update.4

week 2.24-3.01

-Study_update
-Mon继续思考链表,BufferedReader——BufferedWriter的copy功能,
-TueIO流练习,线程的概念,线程的启动_extends
-Wes多线程的运行方式,接口 Runnable,匿名内部类,线程安全问题,synchronized同步方法
-ThusReentrantLock,
-Fri死锁,idea安装,idea界面熟悉,断点操作,搜索功能
-Sat,线程组,Timer定时器,网络编程,ip,端口port,通信协议UDP,TCP
-SunSocket套接字,UDP本机双向聊天,TCP发送信息

2.24 Monday

Copy

public class BufferedReader_WriterCopyTest {
 @Test
 public void copy() {
  BufferedReader reader=null;
  BufferedWriter writer=null;
  try {
   reader=new BufferedReader(new FileReader("Test/demo01.txt"));
   writer=new BufferedWriter(new FileWriter("Test/demo01copy.txt"));
   char[] data=new char[1024];
   while(reader.read(data)>-1) {
    writer.write(data);
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(reader!=null)
    reader.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    if(writer!=null)
    writer.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

读取每行到数组

 /**
  * 读取每行并且放入到List中
  * @author Administrator
  *
  */
 public class IOExerciseDemo {
  public void getDataToArray() {
   BufferedReader reader=null;
   try {
    reader=new BufferedReader(new FileReader("Test/demo01.txt"));
    LinkedList<String> list=new LinkedList<String>();
    String data;
    while((data=reader.readLine())!=null) {
     list.add(data);
    }
    for(String str:list) {
     System.out.println(str);
    }
   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }finally {
    try {
     if(reader!=null)
     reader.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }

2.25 Tuesday

public class CopyAllFileToTheNewFolder {
 //拷贝一个文件夹下的下级所有文件去另外一个文件夹
 public static void copyTo(File source,File targer) {
  File[] list=source.listFiles();//取得下级文件列表
  for(File file:list) {
   if(file.isFile()) {//判断是不是文件
    File newFile=new File(targer,file.getName());//新建一个File 以目标文件夹为父路径,以拷贝的文件为名
    try {
     Files.copy(file.toPath(), newFile.toPath());//拷贝
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 }
}
public class CopyAllFileToTheNewFolder {
 /**
  * 拷贝一个文件夹下的所有文件去另外一个文件夹
  * @param source
  * @param targer
  */
 public static void copyTo(File source,File targer) {
  File[] list=source.listFiles();//取得下级文件列表
  for(File file:list) {
   if(file.isFile()) {//判断是不是文件
    File newFile=new File(targer,file.getName());//新建一个File 以目标文件夹为父路径,以拷贝的文件为名
    try {
     Files.copy(file.toPath(), newFile.toPath());//拷贝
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }else {//
    File newFolder=new File(targer,file.getName());//如果判断是文件,先创建一个同样的文件夹在目标文件夹下,然后调用自身复制这个文件夹下面的文件。
    newFolder.mkdirs();
    copyTo(file, newFolder);
   }
  }
 }
}
 public static void copyTo(File source,File targer) {
  if(targer.exists()==false)//如果文件不存在 创建一个文件夹
   targer.mkdirs();
  File[] list=source.listFiles();//取得下级文件列表
  for(File file:list) {
   if(file.isFile()) {//判断是不是文件
    File newFile=new File(targer,file.getName());//新建一个File 以目标文件夹为父路径,以拷贝的文件为名
    try {
     Files.copy(file.toPath(), newFile.toPath());//拷贝
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }else {//
    File newFolder=new File(targer,file.getName());
    copyTo(file, newFolder);
   }
  }
 }
}
/**
 * 统计文档里面一共有几个目标字符
 * @author Administrator
 *
 */
public class StatisticsTest {
 public static void statistics(File source,char targer) {
  BufferedReader reader=null;
  try {
   reader=new BufferedReader(new FileReader(source));
   char[] data=new char[1024];
   try {
    int length=-1;
    int cnt=0;
    while((length=reader.read(data))>-1) {
     for(int i=0;i<length;i++) {
      if(data[i]=='H') {
       cnt++;
      }
     }
    }
    System.out.println("该文档共有:"+cnt+"个H.");
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally {
   try {
    if(reader!=null)
    reader.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

线程的启动

public class ThreadDemo extends Thread{//继承自Thread
 @Override
 public void run() {//run方法
  for(int i=0;i<100;i++) {
   System.out.println("MyThread: "+i);
  }
 }
}
public class ThreadDemoTestDriver {
 public static void main(String[] args) {
  ThreadDemo myThread=new ThreadDemo();
  myThread.start();//启动线程
  for(int i=0;i<100;i++) {//系统默认线程运行
   System.out.println("SystemThread: "+i);
  }
 }
}

线程的分配,基本是按时间分配或者抢占式分配,比较随机。
优先级越高,cpu更大几率执行。
Priority 优先级

2.26 Wedesday

线程的基本运行过程
在这里插入图片描述
Runnable可以实现数据共享。
Runnable

public class RunableExercise_RoadTicketTestDriver {
 public static void main(String[] args) {
  RunableExercise_RoadTicket r=new RunableExercise_RoadTicket();
  Thread road1=new Thread(r,"售票点一");
  Thread road2=new Thread(r,"售票点二");
  Thread road3=new Thread(r,"售票点三");
  Thread road4=new Thread(r,"售票点四");
  road1.start();
  road2.start();
  road3.start();
  road4.start();
 }
}

------------------
public class RunableExercise_RoadTicket implements Runnable{
 private int ticket=100;
 @Override
 public void run() {
  // TODO Auto-generated method stub
  while(true) {
   if(ticket<=0)
    break;
   System.out.println(Thread.currentThread().getName()+"已出售一张,剩余:"+" "+--ticket+"张."); 
  }
 }
}

Thread

public class Exercise_Road_Thread_TestDriver {
 public static void main(String[] args) {
  Exercise_Road_Thread road1=new Exercise_Road_Thread("一号线: ");
  Exercise_Road_Thread road2=new Exercise_Road_Thread("二号线: ");
  Exercise_Road_Thread road3=new Exercise_Road_Thread("三号线: ");
  Exercise_Road_Thread road4=new Exercise_Road_Thread("四号线: ");
  road1.start();
  road2.start();
  road3.start();
  road4.start();
 }
}
-------------------
public class Exercise_Road_Thread extends Thread{
 private static int ticks=100;
 public Exercise_Road_Thread(String name) {
  // TODO Auto-generated constructor stub
  setName(name);
 }
 @Override
 public void run() {
  while(true) {
   if(ticks<=0)
    break;
   System.out.println(getName()+"已出售一张,剩余:"+" "+--ticks+"张."); 
  } 
 }
}

synchronized 同步的.
使用锁来处理线程问题

public class RunableExercise_RoadTicket implements Runnable{
 private int ticket=100;
 private Object lock=new Object();//锁
 @Override
 public void run() {
  // TODO Auto-generated method stub
  while(true) {
   synchronized (lock) {//如果有在执行这个代码块,锁住,其他线程只能等着该代码块执行完成
    if(ticket>0) {
     System.out.println(Thread.currentThread().getName()+" 已售出第:"+ticket+"张。");
     ticket--;
    }else {
     break;
    }
   }
   try {
    Thread.sleep(100);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
      } 
 }
}

2.27 Thursday

ReentrantLock lock=new ReentrantLock();
lock.lock();
lock.unlock();

==如果有两个线程需要使用同一个变量,但是这两个线程又不是同一个方法类,可以新建一个方法类来存储变量,而且关于线程安全,可在main方法里new一个Object,用来当锁的钥匙,当作参数传输。

public class SellTicketDemoTestDriver {
 public static void main(String[] args) {
  Object lock=new Object();//new一个钥匙
  ByOffline offLine=new ByOffline(lock);//传递
  ByInternet internet=new ByInternet(lock);//传递
  new Thread(offLine,"线下窗口").start();
  new Thread(internet,"网上购买点").start();
 }
}
-------------
public class TicketMng {
 public static int count=100;//共享变量
}
-------------
public class ByOffline implements Runnable{
 public ByOffline(Object lock){//构造函数 传递钥匙
  this.lock=lock;
 }
 public Object lock;
 @Override
 public void run() {
  // TODO Auto-generated method stub
    while(TicketMng.count>0) {
     synchronized(lock) {
      if(TicketMng.count>0) {
       System.out.println(Thread.currentThread().getName()+"已售出第:"+TicketMng.count+"张票。");
       TicketMng.count--;
      }
     }
     try {
      Thread.sleep(100);
     } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
 }
}
------------
public class ByInternet implements Runnable{
 public ByInternet(Object lock){//构造函数 传递钥匙
  this.lock=lock;
 }
 public Object lock;
 @Override
 public void run() {
  // TODO Auto-generated method stub
  while(TicketMng.count>0) {
   synchronized (lock) {
    if(TicketMng.count>0) {
     System.out.println(Thread.currentThread().getName()+"已售出第:"+TicketMng.count+"张票。");
     TicketMng.count--;
    }
   }
   try {
    Thread.sleep(50);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}

2.28 Friday

Live template设置,+ ,新建一个template Group,+,新建一个template,设置简写与template Text.
importSetting 导入设置,可以使用模版设置个性化idea

breakpoints:
常用选项
condition 当达成条件时,停止。
Remove once hit 停止一次后,移除断点。
pass count 循环N次后停止。

2.29 Saturday

线程组,可以批量管理线程,在创建线程的时候分配线程组

/**
 * @author lzr
 * @date 2020/2/29 10:34:29
 * @description
 */
public class TimerDemo {
    public static void main(String[] args) {
        Timer t=new Timer();
        t.schedule(new myTimerTask(),2000,2000);//任务,首次延迟,接下来的延迟
        t.cancel();//结束定时器
    }
}
class myTimerTask extends TimerTask {
    @Override
    public void run() {
        System.out.println("任务");
    }
}

网络
特殊的ip地址 127.0.0.1 localhost

/**
 * @author lzr
 * @data 2020/2/29 18:38:11
 * description
 */
public class Ipdemo {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress ip=InetAddress.getByName("192.168.10.59");//IntetAddress对象
        System.out.println(ip);
    }
}

3.1 Thursday

Socket
在程序中通过Socket进行通信,在使用Socket通信的时候,需要制定上面所说的几个条件(ip,port,协议)

数据发送分为发送端和接收端
发送端一般为客户端,接收端为服务器端。
一般情况下我们会有多个客户端,一个服务器端。

本机UDP通信测试

/**
 * @author lzr
 * @data 2020/3/1 21:23:13
 * description
 */
public class Demo02_UDP_Sent {
    public static void main(String[] args) throws IOException {
        byte[] buf="我是信息包。".getBytes();
        int length=buf.length;
        InetAddress ip=InetAddress.getByName("192.168.1.71");
        int port=7878;
        DatagramSocket ds=new DatagramSocket();
        DatagramPacket dp=new DatagramPacket(buf,length,ip,port);//其中一种构造 字节组,字节组长度,ip地址,端口
        ds.send(dp);//sent需要传递一个DatagramPacket
        ds.close();//必须关闭≤
    }
}
/**
 * @author lzr
 * @data 2020/3/1 21:48:02
 * description
 */
public class Demo3_UDP_Receive {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds=new DatagramSocket(7878);//监听的端口 绑定端口
        byte[] buf=new byte[1024];//存放包裹的数组
        DatagramPacket dp=new DatagramPacket(buf,buf.length);//存放接收数据的包裹
        ds.receive(dp);
        String str=new String(dp.getData(),0,dp.getLength());
        System.out.println(str);
        System.out.println(dp.getAddress());
        System.out.println(dp.getPort());
        ds.close();//必须关闭,一般放在finally
    }
}

TCP:
客户端:Client
服务器端:Server

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值