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();
} }

java新手笔记33 多线程、客户端、服务器的更多相关文章

  1. java新手笔记34 连接数据库

    1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; i ...

  2. Java精选笔记_多线程(创建、生命周期及状态转换、调度、同步、通信)

    线程概述 在应用程序中,不同的程序块是可以同时运行的,这种多个程序块同时运行的现象被称作并发执行. 多线程可以使程序在同一时间内完成很多操作. 多线程就是指一个应用程序中有多条并发执行的线索,每条线索 ...

  3. java新手笔记10 构造器

    1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java. ...

  4. 【原】Java学习笔记032 - 多线程

    package cn.temptation; public class Sample01 { public static void main(String[] args) { /* * [进程]:正在 ...

  5. Java学习笔记之——多线程

    多线程编程 程序: 进程:一个程序运行就会产生一个进程 线程:进程的执行流程,一个进程至少有一个线程,称为主线程 如:QQ聊着天,同时在听音乐 一个进程可以有多个线程,多个线程共享同一个进程的资源 线 ...

  6. Java学习笔记:多线程(一)

    Java中线程的五种状态: 新建状态(New) 就绪状态(Runnable) 运行状态(Running) 阻塞状态(Blocked) 凋亡状态(Dead) 其中阻塞状态(Blocked)又分为三种: ...

  7. JAVA复习笔记之多线程并发

    前言:多线程并发编程是Java编程中重要的一块内容,也是面试重点覆盖区域,还是值得深入研究一下 概念: 1 线程:进程中负责程序执行的执行单元线程本身依靠程序进行运行线程是程序中的顺序控制流,只能使用 ...

  8. java学习笔记(5)多线程

    一.简介(过段时间再写,多线程难度有点大) --------------------------------------- 1.进程:运行时的概念,运行的应用程序 2.线程:应用程序内部并发执行的代码 ...

  9. Java 学习笔记(11)——多线程

    Java内部提供了针对多线程的支持,线程是CPU执行的最小单位,在多核CPU中使用多线程,能够做到多个任务并行执行,提高效率. 使用多线程的方法 创建Thread类的子类,并重写run方法,在需要启动 ...

随机推荐

  1. 【转】linux中的常见目录及文件

    1. /proc目录 Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以 ...

  2. 在.NET中实现彩色光标/动画光标和自定义光标[转]

    下面是完整的例子,可以通过命令行编译即可看到效果. Test.cs using System; using System.Drawing; using System.Windows.Forms; us ...

  3. delphi 获取网页源代码

    //获取网页源代码 var   s: string; begin   s := WebBrowser1.OleObject.document.body.innerHTML; //body内的所有代码 ...

  4. 有图有真相,分享一款网页版HTML5飞机射击游戏

    本飞机射击游戏是使用HTML5代码写的,尝试通过统一开发环境(UDE)将游戏托管在MM应用引擎,直接生成了网页版游戏,游戏简单易上手,非常适合用来当做小休闲打发时间. 游戏地址:http://flyg ...

  5. ios开发——面试篇C语言精华

    面试篇C语言精华    1.面向过程:分析解决问题所需要的步骤,然后用函数把这些步骤一步一步实 现. 面向对象:直接描述客观世界的对象及其相互关系.现实世界中任何实体都 可以看作是对象,对象之间通过消 ...

  6. Android数据的四种存储方式之SQLite数据库

    Test.java: /** * 本例解决的问题: * 核心问题:通过SQLiteOpenHelper类创建数据库对象 * 通过数据库对象对数据库的数据的操作 * 1.sql语句方式操作SQLite数 ...

  7. C加密解密

    /********************************************************* * des.h * 用户使用des算法头文件 * **************** ...

  8. 4K Block Size的Device和 Aligned IO

    http://www.cnblogs.com/cenalulu/p/3587006.html   背景:最近采购了一批新的服务器,底层的存储设备的默认physical sector size从原有的 ...

  9. Ruby on Rails Tutorial 第一章 之 简介

    1.目标:掌握MVC和REST.生成器.迁移.路由.嵌入式Ruby 本书涉及Rails,Ruby语言,Rails默认使用的测试框架(MiniTest),Unix命令行,HTML,CSS,少量的Java ...

  10. sqlserver 字符串相关函数

    http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 以下所有例子均Studnet表为例:  计算字符串长度len()用来 ...