hooyantsing's Blog

第91次课程_网络

字数统计: 934阅读时长: 4 min
2019/12/17

源辰76班

第91次课程

2019.12.17

内容

网络[廖彦]

1.notify()和wait()

notify()

向自身以外的一个线程发送通知

wait()

进入等待状态,等待其他线程的通知,接到通知才能解除等待

Company.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.yc.thread;
import java.util.*;
public class Company {
    private List<Object> list = new  ArrayList<Object>();
    
    public static void main(String[] args) {
       Company c = new Company();
       c.product();
       c.comsume();
    }
    
    public void product() {
       new Thread("生产者") {
           public void run() {
               try {
                  while(true) {
                      synchronized(list) {
                          for(int  i=0;i<10;i++) {
                              System.out.println(Thread.currentThread().getName()+":生产了一个产品");
                              Thread.sleep(200);
                              list.add(new  Object());
                          }
                          //通知消费者线程
                          list.notify();
                          //等待消费者线程通知
                          list.wait();
                      }
                  }
               } catch (Exception e) {
                  e.printStackTrace();
               }
           }
       }.start();
    }
    
    public void comsume() {
       new Thread("消费者") {
           public void run() {
               try {
                  while(true) {
                      synchronized (list) {
                          while(list.size()>0) {
                              System.out.println(Thread.currentThread().getName()+":消费了一个产品");
                              Thread.sleep(100);
                              list.remove(0);
                          }
                          //通知生产者线程
                          list.notify();
                          //等待消费者线程通知
                          list.wait();
                      }
                  }
               } catch (Exception e) {
                  e.printStackTrace();
               }
           }
       }.start();
    }
}

2.网络编程

URL类的应用

Demo1.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.yc.net;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class Demo1 {
    
    /**
     * 网络编程
     * ISO
     *
     * TCP/IP
     *
     * IP 是计算机在网络中的数字地址
     * 端口:0~65535 1024以下的端口是预留给系统的
     */
    
    /**
     * URL 类:用于访问网络上的资源
     * Socket 类:用于两个计算机之间的通信
     */

    public static void main(String[] args)  throws IOException {
       //URL 全局资源定位器
       URL url = new  URL("http://www.hyycinfo.com/index.html");
       URLConnection conn =  url.openConnection();
       System.out.println(conn);
       
       /*
       conn.getContentType();//网络资源类型
       conn.getLastModified();   //最后修改时间
       conn.getContentLength();//获取大小
       */
       
       InputStream in =  conn.getInputStream();
       byte[] buffer = new byte[1024];
       int count;
       while((count = in.read(buffer)) > 0)  {
           //0 -> count :为了防止最后一次读取时,误读到垃圾数据
           String s = new  String(buffer,0,count);
           System.out.println(s);
       }
    }
}

3.[案例]即时聊天室

Socket类的应用

Client.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.yc.net;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;
public class Client {
    
    public static void main(String[] args)  throws IOException {
       Socket socket = new  Socket("127.0.0.1",8888);
       
       //获取客户端地址对象
       InetAddress addr =  socket.getInetAddress();
       System.out.println(addr.getHostName());
       System.out.println(Arrays.toString(addr.getAddress()));
       
       InputStream in =  socket.getInputStream();
       OutputStream out =  socket.getOutputStream();
       Scanner sc = new Scanner(System.in);
       
       //读线程
       new Thread() {
           public void run() {
               while (true) {
                  try {
                      byte[] buffer = new  byte[1024];
                      int count;
                      count =  in.read(buffer);
                      String s = new  String(buffer, 0, count);
                      System.out.println(s);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
               }
           }
       }.start();
       //写线程
       new Thread() {
           public void run() {
               while (true) {
                  try {
                      out.write(sc.nextLine().getBytes());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
               }
           }
       }.start();
    }
}

Server.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.yc.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.util.Scanner;
public class Server {
    public static void main(String[] args)  throws IOException {
       // 创建Socket服务端
       ServerSocket server = new  ServerSocket(8888);
       System.out.println("服务器启动成功,等待客户端连接:8888");
       // server等待客户端连接,进入阻塞状态
       Socket socket = server.accept();
       // 获取客户端地址对象
       InetAddress addr =  socket.getInetAddress();
       System.out.println(addr.getHostName());
       System.out.println(Arrays.toString(addr.getAddress()));
       InputStream in =  socket.getInputStream();
       OutputStream out =  socket.getOutputStream();
       Scanner sc = new Scanner(System.in);
       out.write("欢迎来到Hooy聊天室".getBytes());
       new Thread() {
           public void run() {
               while (true) {
                  try {
                      byte[] buffer = new  byte[1024];
                      int count;
                      count =  in.read(buffer);
                      String s = new  String(buffer, 0, count);
                      System.out.println(s);
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
               }
           }
       }.start();
       new Thread() {
           public void run() {
               while (true) {
                  try {
                      out.write(sc.nextLine().getBytes());
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
               }
           }
       }.start();
       /*
        * 存在问题 已作废 byte[] buffer = new  byte[1024]; int count = in.read(buffer);  String
        * s = new String(buffer, 0, count);  System.out.println(s);
        */
    }
}
CATALOG