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方法,在需要启动 ...
随机推荐
- pod install warning
warning: Insecure world writable dir /usr/local/bin in PATH, mode 040777 解决方法: sudo chmod 775 /usr/l ...
- C++ 预编译头文件
1.解决什么问题? C++ 编译器是单独,分别编译的,每个cpp文件,进行预编译(也就是对#include,define 等进行文本替换),生成编译单元.编译单元是一个自包含文件,C++编译器对编译单 ...
- thinkphp中的HTTP类实现下载
public function test(){ import('ORG.Net.Http'); $filename="Uploads/v1.2.doc"; //exit($file ...
- jsp forward 动作标签
forward 动作标签: <jsp:forward page="要转向的页面"> </jsp:forward> 或 <jsp:forward pag ...
- [MODx] 2. Install some useful packages into ur MODx
1. The package we might need: 2. Install the package: Select Installer Download Extras Install the p ...
- NDK环境配置
1.下载安装插件:com.android.ide.eclipse.ndk_23.0.2.1259578.jar copy到E:\eclipse\adt-bundle-windows-x86- ...
- java中接口的定义与实现
1.定义接口 使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,当中接口体由常量定义和方法定义两部分组成.定义接口的基本格式例如以下: [修饰符] in ...
- 使用Java编写并运行Spark应用程序
我们首先提出这样一个简单的需求: 现在要分析某网站的访问日志信息,统计来自不同IP的用户访问的次数,从而通过Geo信息来获得来访用户所在国家地区分布状况.这里我拿我网站的日志记录行示例,如下所示: 1 ...
- textarea 中的换行符问题
下面是我对这个问题的解决过程,最后算是完全搞懂了,真是阴沟里险些翻船 1.必须知道textarea中的换行符是 \n (个人检测发现按回车键是\n,好像在linux下是\r\n) 2.用nl2br之 ...
- Android基本控件之RadioGroup
我们在手机上经常看到一堆选项,但是我们只能选择一个,那么在Android中,这个控件就叫做RadioButton,也就是单选按钮的意思,他们之所以能够达到只能选择一个的效果,是因为有一个RadioGr ...