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();
} }
java新手笔记33 多线程、客户端、服务器的更多相关文章
- java新手笔记34 连接数据库
1.JdbcUtil package com.yfs.javase.jdbc; import java.sql.Connection; import java.sql.DriverManager; i ...
- Java精选笔记_多线程(创建、生命周期及状态转换、调度、同步、通信)
线程概述 在应用程序中,不同的程序块是可以同时运行的,这种多个程序块同时运行的现象被称作并发执行. 多线程可以使程序在同一时间内完成很多操作. 多线程就是指一个应用程序中有多条并发执行的线索,每条线索 ...
- java新手笔记10 构造器
1.摇奖小程序 package com.yfs.javase; import java.io.IOException; import java.nio.CharBuffer; import java. ...
- 【原】Java学习笔记032 - 多线程
package cn.temptation; public class Sample01 { public static void main(String[] args) { /* * [进程]:正在 ...
- Java学习笔记之——多线程
多线程编程 程序: 进程:一个程序运行就会产生一个进程 线程:进程的执行流程,一个进程至少有一个线程,称为主线程 如:QQ聊着天,同时在听音乐 一个进程可以有多个线程,多个线程共享同一个进程的资源 线 ...
- Java学习笔记:多线程(一)
Java中线程的五种状态: 新建状态(New) 就绪状态(Runnable) 运行状态(Running) 阻塞状态(Blocked) 凋亡状态(Dead) 其中阻塞状态(Blocked)又分为三种: ...
- JAVA复习笔记之多线程并发
前言:多线程并发编程是Java编程中重要的一块内容,也是面试重点覆盖区域,还是值得深入研究一下 概念: 1 线程:进程中负责程序执行的执行单元线程本身依靠程序进行运行线程是程序中的顺序控制流,只能使用 ...
- java学习笔记(5)多线程
一.简介(过段时间再写,多线程难度有点大) --------------------------------------- 1.进程:运行时的概念,运行的应用程序 2.线程:应用程序内部并发执行的代码 ...
- Java 学习笔记(11)——多线程
Java内部提供了针对多线程的支持,线程是CPU执行的最小单位,在多核CPU中使用多线程,能够做到多个任务并行执行,提高效率. 使用多线程的方法 创建Thread类的子类,并重写run方法,在需要启动 ...
随机推荐
- stack对象与heap对象
从高地址到低地址,分别是stack,heap,static object,stack地址往下增长,heap地址往上增长.只要记住:stack栈顶地址反而小,就知道往下增长了. 禁止产生堆对象 1.产生 ...
- 套题 Codeforces Round #277 (Div. 2)
A. Calculating Function 水题,分奇数偶数处理一下就好了 #include<stdio.h> #include<iostream> using names ...
- HDU 4821 String hash
String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- SkinSharp用法
SkinSharp又称Skin#,是很好用的一款轻量化的VC程序美化工具 官网地址是http://www.skinsharp.com/ 尽管SkinSharp是收费软件,但提供试用版,并且比較厚道,试 ...
- iOS开发——图形与动画篇OC篇&图层基本上动画
图层的一些基本动画效果 #define kRadianToDegrees (radian) (radian * 180.0) / (M_PI) //闪烁 [self.testView.layer ad ...
- JavaScipt call和apply用法
转:http://www.cnblogs.com/wupeng/p/3477879.html Javascript call与apply记录 [注]:记录自己对javascript中call与appl ...
- 微信朋友圈分享页面(JS-SDK 1.0)
微信更新sdk后大量分享朋友圈代码失效,标题 缩略图 描述无法自定义 新版SDK分享文章步骤 1.绑定域名 (方法参考 http://mp.weixin.qq.com/wiki/7/aaa137b55 ...
- Qt之遍历文件夹
关于Qt操作文件夹.文件的知识用途较多,比如遍历下一层乃至所有子孙文件.文件夹,获取它们的一些信息(大小.类型.最后更改时间等).当然,也可以进行级联删除. 首先看简单的: 一.Qt遍历文 ...
- iOS开发之 动画CoreAnimation
http://blog.treney.com/index.php/archives/CoreAnimation.html?hmsr=toutiao.io&utm_medium=toutiao. ...
- Starship Troopers
Problem Description You, the leader of Starship Troopers, are sent to destroy a base of the bugs. Th ...