zoukankan      html  css  js  c++  java
  • java新手笔记33 多线程、客户端、服务器

    1.Mouse

    package com.yfs.javase;
    
    public class Mouse {
    	
    	private int index = 1;
    	private boolean isLive = false;
    	//跳出方法  同步锁
    	public synchronized void jump() {
    		while(true) {
    			if(!isLive ) {
    				System.out.println("跳出第 " + index + " 田鼠");
    				//修改田鼠状态
    				isLive = true;
    				//通知打的线程
    				//notify();				
    			} else {
    				//当前线程等待
    //				try {
    //					wait();
    //				} catch (InterruptedException e) {
    //					e.printStackTrace();
    //				}
    			}
    		}
    	}
    	public synchronized void hit() {
    		while(true) {
    			if(isLive) {
    				System.out.println("打掉第 " + index + " 田鼠..");
    				index ++;
    				isLive = false;
    				//notify();//通知放田鼠的线程
    			} else {
    //				try {
    //					//wait();
    //				} catch (InterruptedException e) {
    //					e.printStackTrace();
    //				}
    			}
    		}
    	}
    
    }
    

     2.Producer

    package com.yfs.javase;
    
    public class Producer extends Thread {
    	private Mouse mouse;
    	
    	public Producer(Mouse  mouse) {
    		this.mouse = mouse;
    	}
    	
    	@Override
    	public void run() {
    		mouse.jump();
    	}
    
    }
    

     3.Customer

    package com.yfs.javase;
    
    public class Customer extends Thread {
    	private Mouse mouse;
    	public Customer(Mouse mouse) {
    		this.mouse = mouse;
    	}
    	
    	@Override
    	public void run() {
    		mouse.hit();
    	}
    	
    
    }
    

     4.MouseTest

    package com.yfs.javase;
    
    public class MouseTest {
    
    	public static void main(String[] args) {
    		Mouse mouse = new Mouse();
    		new Producer(mouse).start();
    		new Customer(mouse).start();
    	}
    
    }
    

     5.Ticket

    package com.yfs.javase;
    
    public class Ticket implements Runnable {
    	private int index = 100;//共享资源
    
    	//锁机制
    	@Override
    	public void run() {
    		
    		while(index > 0) {
    			synchronized (this) {//同步锁  当前对象
    				try {
    					Thread.sleep(3);
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    				if(index <= 0) { 
    					break;
    				}
    				System.out.println(Thread.currentThread().getName() + "  第  " + index  +  "  票售出");
    				index --;
    			}
    			
    		}
    		
    	}
    
    }
    

     6.TicketTest

    package com.yfs.javase;
    
    public class TicketTest {
    
    	public static void main(String[] args) {
    		Ticket tickets = new Ticket();
    		new Thread(tickets,"1号").start();
    		new Thread(tickets,"2号").start();
    		new Thread(tickets,"3号").start();
    		new Thread(tickets,"4号").start();
    
    	}
    
    }
    

     7.IP 地址/主机名称

    package com.yfs.javase.net;
    
    import java.net.InetAddress;
    
    public class AddressDemo {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    		InetAddress add1 = InetAddress.getLocalHost();
    		System.out.println("IP 地址 :" + add1.getHostAddress());
    		System.out.println("主机名称 :" + add1.getHostName());
    		
    		InetAddress add2 = InetAddress.getByName("www.baidu.com");
    		System.out.println("IP 地址 :" + add2.getHostAddress());
    		System.out.println("主机名称 :" + add2.getHostName());
    		
    
    	}
    
    }
    

     8.Server1

    package com.yfs.javase.net;
    
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server1 {
    
    	/**
    	 * 启动服务 监听端口
    	 */
    	public static void main(String[] args) throws Exception {
    		ServerSocket server = new ServerSocket(3000);
    		//启动
    		System.out.println("服务器启动,监听3000端口...");
    		Socket socket = server.accept();//监听是否有其他主机连接
            String other = socket.getInetAddress().getHostAddress();
    		System.out.println(other + "请求连接...");
    		
    		//发送信息   获取输出流
    		OutputStream out = socket.getOutputStream();
    		out.write('a');
    		out.flush();//刷新
    		out.close();
    		System.out.println("信息发送完成");
    		
    		
    		
    		
    	}
    
    }
    

     9.Client1

    package com.yfs.javase.net;

    import java.io.InputStream;
    import java.net.Socket;

    public class Client1 {

        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
            Socket socket = new Socket("192.168.1.30", 3000);
            //接收信息
            InputStream in = socket.getInputStream();
            int val = in.read();
            System.out.println("val = " + (char)val);
            in.close();
        }

    }

     10.Server2

    package com.yfs.javase.net;
    
    import java.io.BufferedOutputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class Server2 {
    
    	/**
    	 * 启动服务 监听端口
    	 */
    	public static void main(String[] args) throws Exception {
    		ServerSocket server = new ServerSocket(3000);
    		//启动
    		System.out.println("服务器启动,监听3000端口...");
    		Socket socket = server.accept();//监听是否有其他主机连接
            String other = socket.getInetAddress().getHostAddress();
    		System.out.println(other + "请求连接...");
    		
    		//发送信息   获取输出流
    		OutputStream out = socket.getOutputStream();
    		BufferedOutputStream buf = new BufferedOutputStream(out);
    		PrintWriter pw = new PrintWriter(new OutputStreamWriter(buf));
    	    pw.println("同学们,你们好! 这是服务器发送的信息....");
    	    pw.close();
    	    buf.close();
    		out.close();
    		System.out.println("信息发送完成");
    	}
    
    }
    

     11.Client2

    package com.yfs.javase.net;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.Socket;
    
    public class Client2 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    		Socket socket = new Socket("192.168.1.30", 3000);
    		//接收信息
    		InputStream in = socket.getInputStream();
    		BufferedReader read = new BufferedReader(new InputStreamReader(in));
    		String msg = read.readLine();
    		System.out.println("服务器说:" + msg);
    		read.close();
    		in.close();
    	}
    
    }
    

     12.Server3

    package com.yfs.javase.net;
    
    import java.io.BufferedOutputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.util.Scanner;
    
    public class Server3 {
    
    	/**
    	 * 启动服务 监听端口
    	 */
    	public static void main(String[] args) throws Exception {
    		ServerSocket server = new ServerSocket(3000);
    		//启动
    		System.out.println("服务器启动,监听3000端口...");
    		Socket socket = server.accept();//监听是否有其他主机连接
            String other = socket.getInetAddress().getHostAddress();
    		System.out.println(other + "请求连接...");
    		
    		//发送信息   获取输出流
    		OutputStream out = socket.getOutputStream();
    		BufferedOutputStream buf = new BufferedOutputStream(out);
    		PrintWriter pw = new PrintWriter(new OutputStreamWriter(buf));
    	    pw.println("同学们,你们好! 这是服务器发送的信息....");
    	    pw.flush();
    	    Scanner scan = new Scanner(System.in);
    	    String msg = null;
    	    int i = 1;
    	    while(i < 3000) {
    	    	msg = scan.next();
    	    	pw.println(msg);
    	    	pw.flush();
    	    	i++;
    	    }
    	    pw.close();
    	    buf.close();
    		out.close();
    		System.out.println("信息发送完成");
    	}
    
    }
    

     13.Client3

    package com.yfs.javase.net;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.Socket;
    
    public class Client3 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    		Socket socket = new Socket("192.168.1.30", 3000);
    		//接收信息
    		InputStream in = socket.getInputStream();
    		BufferedReader read = new BufferedReader(new InputStreamReader(in));
    		String msg = read.readLine();
    		System.out.println("服务器说:" + msg);
    		int i = 1;
    		while(i < 3000) {
    			msg = read.readLine();
    			System.out.println("服务器说:" + msg);
    			i++;
    		}		
    		read.close();
    		in.close();
    	}
    
    }
    
  • 相关阅读:
    数据结构之 移位操作
    大话设计模式之外观模式
    JSP的内置对象(application)
    从键盘输入一个整数(1~20) 则以该数字为矩阵的大小,把1,2,3…n*n 的数字按照顺时针螺旋的形式填入其中。
    linux线程应用
    【网络挖掘:成就与未来方向】之网络挖掘应用程序与相关概念
    Thinking in Java之匿名内部类
    [Go] map
    [跟着hsp步步学习系统]oracle培训学习集锦全360度扫描(2)
    HDU3791:二叉搜索树
  • 原文地址:https://www.cnblogs.com/feilongblog/p/4758935.html
Copyright © 2011-2022 走看看