執行緒通信
多執行緒之間打成通信溝通的效果,協作完成業務需求
Object:
wait() 執行緒等待 當呼叫某一個物件 的wait方法,當前執行緒就會進入到與這個物件相關的等待池中進行等待--->等待阻塞,等待被喚醒
會讓出cpu的資源,并且會釋放物件的鎖
notify() 喚醒執行緒 當呼叫一個物件的notify方法,會喚醒當前物件等待池中正在等待的執行緒,喚醒某一個
這個執行緒會進入到就緒狀態,要想要運行: 1)cpu的調度 2)獲取物件鎖
wait(ms) 阻塞等待指定時間
notifyAll() 喚醒全部
wait與notify必須使用在一個同步環境下,用于控制多執行緒之間協調作業問題,保證資料安全
sleep與wait之間區別
sleep : 執行緒休眠 抱著資源睡覺: 讓出cpu資源,抱著物件的鎖
人車共用街道:
街道 : 紅綠燈 boolean flag 綠燈-->人走 true 紅燈-->車走 false ns南北走向 we東西走向
人 : ns南北
車 : we東西
生產者消費者模式:
通過信號燈法
*/
public class Class001_Wait {
public static void main(String[] args) {
Street street = new Street(); //共享街道
new Thread(new Person(street)).start();
new Thread(new Car(street)).start();
}
}
//街道
class Street{
//紅綠燈
private boolean flag = false;
//ns
public synchronized void ns(){
//判斷是否為綠燈
if(flag){
/* try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println("人走......");
//紅綠燈變為紅燈
flag=false;
//喚醒對方執行緒
this.notify();
//自己等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//we
public synchronized void we(){
if(!flag){
/*try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
System.out.println("車走......");
//紅綠燈變為紅燈
flag=true;
//喚醒對方執行緒
this.notify();
//自己等待
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//人
class Person implements Runnable{
//街道
private Street street = null;
public Person(Street street) {
this.street = street;
}
@Override
public void run() {
while(true){
street.ns();
}
}
}
//車
class Car implements Runnable{
//街道
private Street street = null;
public Car(Street street) {
this.street = street;
}
@Override
public void run() {
while(true){
street.we();
}
}
}
網頁編程 : 上層的應用
網路編程 : 底層,關注資料如何傳輸,如何存盤
節點 : 網路電子設備
節點與節點之間組成網路
IP : 表示節點
埠 : 區分不同的軟體
URL : 互聯網中資源的指標,統一資源定位符
協議 : 合同,標準,規范
傳輸層協議 :
UDP : 相當于寫信 只管寫只管發 效率高 不安全 大小存在限制
TCP : 相當于打電話 面向連接 安全性高 效率低 大小沒有限制 ****
IP :
定義網路中的節點 (網路電子設備,手機,電腦,路由器...)
分為 : IPV4(4個位元組,32位) IPV6 (128位)
特殊IP:
192.168.0.0~192.168.255.255 非注冊IP,供組織內部使用
127.0.0.1 本地IP
localhost : 本地域名
域名與IP之間的關系: DNS決議器
java.net包
InetAddress 類表示Internet協議(IP)地址
*/
public class Class001_IP {
public static void main(String[] args) throws UnknownHostException {
//static InetAddress getByName(String host) 根據主機名稱確定主機的IP地址,
//static InetAddress getLocalHost() 回傳本地主機的地址,
InetAddress address1 = InetAddress.getLocalHost();
System.out.println(address1); //DESKTOP-KHNV6UD/192.168.16.236
System.out.println(address1.getHostName());
System.out.println(address1.getHostAddress());
InetAddress address2 = InetAddress.getByName("www.baidu.com");
System.out.println(address2);
System.out.println(address2.getHostName());
System.out.println(address2.getHostAddress());
}
}
IP : 定位節點
埠 : 區分軟體
埠號 2個位元組 0~65535
同一協議下埠號不能沖突
建議使用8000以上的,8000以下稱為預留埠號
常見的埠:
80 : http
8080 : tomcat
1521 : Oracle
3306 : Mysql
InetSocketAddress 此類實作IP套接字地址(IP地址+埠號)它也可以是一對(主機名+埠)
public class Class002_Port {
public static void main(String[] args) {
//InetSocketAddress(String hostname, int port) 根據主機名和埠號創建套接字地址,
//InetSocketAddress(InetAddress addr, int port) 根據IP地址和埠號創建套接字地址,
InetSocketAddress in = new InetSocketAddress("localhost",8989);
System.out.println(in);
System.out.println(in.getHostName());
System.out.println(in.getPort());
}
}
URL
同一資源定位符,指向萬維網上的“資源”的指標,
組成:
協議: http
域名: www.baidu.com
埠號: 80
資源: index.html
提交資料: name=zhangsan&pwd=123
錨點: #a
互聯網 的三大基石:
html
http
url
URL 類
public class Class003_URL {
public static void main(String[] args) throws MalformedURLException {
//URL(String spec) 從 String表示創建 URL物件,
//URL(String protocol, String host, int port, String file)
URL url = new URL("https://www.baidu.com:80/index.html?name=zhangsan&pwd=123#a");
System.out.println(url);
System.out.println("協議:"+url.getProtocol());
System.out.println("域名:"+url.getHost());
System.out.println("埠:"+url.getPort());
System.out.println("資源:"+url.getFile());
System.out.println("資源:"+url.getPath());
System.out.println("資源:"+url.getQuery());
System.out.println("資源:"+url.getRef());
}
}
網路爬蟲
public class Class004_Spider {
public static void main(String[] args) throws IOException {
//1.定義URL
URL url = new URL("https://www.baidu.com/index.html");
//2.獲取流 : InputStream openStream() 打開與此 URL的連接并回傳 InputStream以從該連接讀取,
InputStream is = url.openStream();
//InputStreamReader 位元組輸入流轉位字符輸入流的節點流-->功能流
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String msg = null;
//3.讀入操作
while((msg = rd.readLine())!=null){
System.out.println(msg);
}
//4.關閉
rd.close();
is.close();
}
}
套接字:
傳輸層為應用層開辟的小口子
不同協議下Socket實作不同
UDP與TCP協議對Socket實作
UDP : 相當于寫信|有包裹|發短信 非面向連接 協議簡單,開銷小,效率高 不安全 大小由限制(一般不超過60k)
TCP : 相當于打電話 面向連接 效率低 安全 大小沒有限制
基于三次握手
UDP協議下發送端與接收端兩端平等
DatagramSocket 此類表示用于發送和接收資料報包的套接字,
DatagramSocket(int port) 構造一個資料報套接字并將其系結到本地主機上的指定埠,
void receive(DatagramPacket p) 從此套接字接收資料報包,
void send(DatagramPacket p) 從此套接字發送資料報包,
DatagramPacket 該類表示資料報包,
byte[] getData() 回傳資料緩沖區,
int getLength() 回傳要發送的資料的長度或接收的資料的長度,
資料的傳輸基于位元組陣列
UDP實作發送端: 基本流程
1.定義我是發送端
2.準備資料
3.打包
4.發送
5.關閉
*/
public class Class001_Send {
public static void main(String[] args) throws IOException {
// 1.定義我是發送端
DatagramSocket s = new DatagramSocket(9090);
System.out.println("-------------我是發送端-------------");
// 2.準備資料
byte[] arr = "你好".getBytes();
// 3.打包
DatagramPacket packet = new DatagramPacket(arr,0,arr.length,new InetSocketAddress("127.0.0.1",8989));
// 4.發送
s.send(packet);
// 5.關閉
s.close();
}
}
public class Class002_Receive {
public static void main(String[] args) throws IOException {
//1.定義我是接收端
DatagramSocket r = new DatagramSocket(8989);
System.out.println("-----------我是接收端------------");
//2.準備位元組陣列,打包
byte[] arr = new byte[1024];
DatagramPacket packet = new DatagramPacket(arr,arr.length);
//3.接收資料
r.receive(packet);
//4.處理資料
//byte[] getData() 回傳資料緩沖區,
//int getLength() 回傳要發送的資料的長度或接收的資料的長度,
byte[] newArr = packet.getData();
int len = packet.getLength();
System.out.println(new String(newArr,0,len));
//5.關閉
r.close();
}
}
客戶端 Socket
Socket(String host, int port) 創建流套接字并將其連接到指定主機上的指定埠號,
InputStream getInputStream()
OutputStream getOutputStream()
服務器 ServerSocket 該類實作服務器套接字,
ServerSocket(int port) 創建系結到指定埠的服務器套接字,
Socket accept() 偵聽對此套接字的連接并接受它,
tcp協議下傳輸資料基于IO流
tcp協議實作基本流程 : 客戶端
1.定義我是客戶端-->指定要請求的服務器的IP+埠
2.準備資料
3.獲取輸出流
4.輸出-->IO操作
5.刷出
6.關閉
*/
public class Class001_Client {
public static void main(String[] args) throws IOException {
System.out.println("-----------我是客戶端--------------");
// 1.定義我是客戶端-->指定要請求的服務器的IP+埠
Socket client = new Socket("localhost",9999);
System.out.println("-----------與服務器端建立連接--------------");
// 2.準備資料
String str = "你好";
// 3.獲取輸出流
DataOutputStream os = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
// 4.輸出-->IO操作
os.writeUTF(str);
// 5.刷出
os.flush();
// 6.關閉
os.close();
client.close();
}
}
tcp協議實作基本流程 : 服務端
1.定義我是服務端
2.阻塞式監聽
3.獲取輸入流-->接收客戶端的請求資料
4.處理資料
5.關閉
*/
public class Class002_Server {
public static void main(String[] args) throws IOException {
System.out.println("-----------我是服務器端-----------");
// 1.定義我是服務端
ServerSocket server = new ServerSocket(9999);
// 2.阻塞式監聽
Socket client = server.accept();
System.out.println("-----------一個客戶端連接成功----------");
// 3.獲取輸入流-->接收客戶端的請求資料
DataInputStream is = new DataInputStream(new BufferedInputStream(client.getInputStream()));
String msg = is.readUTF();
// 4.處理資料
System.out.println(msg);
// 5.關閉
is.close();
client.close();
server.close();
}
}
tcp 單向登錄: 客戶端
1.定義客戶端
2.準備資料(用戶輸入)
1)輸入流
2)用戶名與密碼
3.獲取輸出流向服務器端發送資料(用戶名與密碼)
4.刷出
5.關閉
*/
public class Class003_LoginClient {
public static void main(String[] args) throws IOException {
System.out.println("-------我是客戶端---------");
//1.定義客戶端
Socket client = new Socket("localhost",9898);
//2.準備資料(用戶輸入)
// 1)輸入流
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
// 2)用戶名與密碼
System.out.println("請輸入用戶名");
String username = rd.readLine();
System.out.println("請輸入密碼");
String password = rd.readLine();
System.out.println(username+"-->"+password);
//3.獲取輸出流向服務器端發送資料(用戶名與密碼)
DataOutputStream os = new DataOutputStream(client.getOutputStream());
//username=laopei&password=1234
os.writeUTF("username="+username+"&password="+password);
//4.刷出
os.flush();
//5.關閉
os.close();
rd.close();
client.close();
}
}
tcp 單向登錄: 服務端
1.定義我是服務器
2.阻塞式監聽
3.獲取輸入流接收客戶端發動的資料
4.處理資料
5.關閉
要求: 服務器端接收到用戶輸入的用戶名與密碼,與指定的laopei,1234比較是否相等,相等本地輸出登錄成功,不相等輸出用戶名或密碼錯誤!!!
*/
public class Class003_LoginServer {
public static void main(String[] args) throws IOException {
System.out.println("--------我是服務器-------");
//1.定義我是服務器
ServerSocket server = new ServerSocket(9898);
//2.阻塞式監聽
Socket client = server.accept();
System.out.println("一個客戶端連接成功........");
//3.獲取輸入流接收客戶端發動的資料
DataInputStream is = new DataInputStream(client.getInputStream());
String msg = is.readUTF(); //username=laopei&password=1234
//4.處理資料
System.out.println(msg);
//5.關閉
is.close();
client.close();
server.close();
}
}
tcp 雙向登錄: 客戶端
1.定義客戶端
2.準備資料(用戶輸入)
1)輸入流
2)用戶名與密碼
3.獲取輸出流向服務器端發送資料(用戶名與密碼)
4.刷出
5.獲取輸入流 從服務器端讀取回應
6.關閉
*/
public class Class005_LoginTwoWayClient {
public static void main(String[] args) throws IOException {
System.out.println("-------我是客戶端---------");
//1.定義客戶端
Socket client = new Socket("localhost",9898);
//2.準備資料(用戶輸入)
// 1)輸入流
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
// 2)用戶名與密碼
System.out.println("請輸入用戶名");
String username = rd.readLine();
System.out.println("請輸入密碼");
String password = rd.readLine();
System.out.println(username+"-->"+password);
//3.獲取輸出流向服務器端發送資料(用戶名與密碼)
DataOutputStream os = new DataOutputStream(client.getOutputStream());
//username=laopei&password=1234
os.writeUTF("username="+username+"&password="+password);
//4.刷出
os.flush();
//5.獲取輸入流 從服務器端讀取回應
DataInputStream is = new DataInputStream(client.getInputStream());
System.out.println(is.readUTF());
//6.關閉
is.close();
os.close();
rd.close();
client.close();
}
}
tcp 雙向登錄: 服務端
1.定義我是服務器
2.阻塞式監聽
3.獲取輸入流接收客戶端發動的資料
4.處理資料
5.獲取輸出流 把結果回應 給客戶端
6.刷出
7.關閉
要求: 服務器端接收到用戶輸入的用戶名與密碼,與指定的laopei,1234比較是否相等,相等本地輸出登錄成功,不相等輸出用戶名或密碼錯誤!!!
*/
public class Class006_LoginTwoWayServer {
public static void main(String[] args) throws IOException {
System.out.println("--------我是服務器-------");
//1.定義我是服務器
ServerSocket server = new ServerSocket(9898);
//2.阻塞式監聽
Socket client = server.accept();
System.out.println("一個客戶端連接成功........");
//3.獲取輸入流接收客戶端發動的資料
DataInputStream is = new DataInputStream(client.getInputStream());
String msg = is.readUTF(); //username=laopei&password=1234
//4.處理資料
System.out.println(msg);
//處理1)
/*String str = "username=laopei&password=1234";
if(str.equals(msg)){
System.out.println("登錄成功");
}else{
System.out.println("登錄失敗");
}*/
//2)
String username = null;
String password = null;
String[] strs = msg.split("&");
for(String s:strs){
String[] arr = s.split("=");
if("username".equals(arr[0])){
username = arr[1];
}else if("password".equals(arr[0])){
password = arr[1];
}
}
//5.獲取輸出流 把結果回應 給客戶端
DataOutputStream os = new DataOutputStream(client.getOutputStream());
if("laopei".equals(username) && "1234".equals(password)){
os.writeUTF("登錄成功");
}else{
os.writeUTF("登錄失敗");
}
//6.刷出
os.flush();
//7.關閉
os.close();
is.close();
client.close();
server.close();
}
}
多用戶登錄服務器端
通過回圈可以實作多用戶登錄
但是服務器只能排隊對不同的客戶端做回應
*/
public class Class007_MulLoginTwoWayServer {
public static void main(String[] args) throws IOException {
System.out.println("--------我是服務器-------");
//1.定義我是服務器
ServerSocket server = new ServerSocket(9898);
//2.阻塞式監聽
boolean flag = true;
while(flag){
Socket client = server.accept();
System.out.println("一個客戶端連接成功........");
//3.獲取輸入流接收客戶端發動的資料
DataInputStream is = new DataInputStream(client.getInputStream());
String msg = is.readUTF();
//4.處理資料
String username = null;
String password = null;
String[] strs = msg.split("&");
for(String s:strs){
String[] arr = s.split("=");
if("username".equals(arr[0])){
username = arr[1];
}else if("password".equals(arr[0])){
password = arr[1];
}
}
//5.獲取輸出流 把結果回應 給客戶端
DataOutputStream os = new DataOutputStream(client.getOutputStream());
if("laopei".equals(username) && "1234".equals(password)){
os.writeUTF("登錄成功");
}else{
os.writeUTF("登錄失敗");
}
//6.刷出
os.flush();
//7.關閉
os.close();
is.close();
client.close();
}
server.close();
}
}
多用戶登錄服務器端
通過多執行緒實作
*/
public class Class008_MulLoginTwoWayServer {
public static void main(String[] args) throws IOException {
System.out.println("--------我是服務器-------");
//1.定義我是服務器
ServerSocket server = new ServerSocket(9898);
//2.阻塞式監聽
boolean flag = true;
while(flag){
Socket client = server.accept();
System.out.println("一個客戶端連接成功........");
//開啟執行緒為上面監聽到客戶端回應
new Thread(new Channel(client)).start();
}
server.close();
}
static class Channel implements Runnable{
private Socket client = null;
private DataInputStream is = null;
private DataOutputStream os = null;
public Channel(Socket client) {
this.client = client;
try {
is = new DataInputStream(client.getInputStream());
os = new DataOutputStream(client.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
//讀入資料
public String read(){
String msg = null;
try {
msg = is.readUTF();
} catch (IOException e) {
e.printStackTrace();
}
return msg;
}
//寫出
public void write(String msg){
try {
os.writeUTF(msg);
os.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
//關閉資源
public void close(){
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(client!=null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
//獲取輸入流接收客戶端發動的資料
String msg = read();
//處理資料
String username = null;
String password = null;
String[] strs = msg.split("&");
for(String s:strs){
String[] arr = s.split("=");
if("username".equals(arr[0])){
username = arr[1];
}else if("password".equals(arr[0])){
password = arr[1];
}
}
//獲取輸出流 把結果回應 給客戶端
if("laopei".equals(username) && "1234".equals(password)){
write("登錄成功");
}else{
write("登錄失敗");
}
//關閉
close();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/290019.html
標籤:其他
