JAVA : Socket和多线程
来源:互联网 发布:郑州财经学院网络下线 编辑:程序博客网 时间:2024/05/17 22:46
这几天面试老是被问到你对Java并发和网络编程熟不熟。
姐比较实诚,我直明,看过资料,没动过手,于是被鄙视之。 = =以后我的博客应该叫荒废一年的IT女码农边学边面。。。
干脆回来花了点时间写了程序,练个手。
Socket Server 处理多个Socket client的写入请求,然后Socket Server将读取的数据写入一个和client共享的Queue中。也就是server是这个Queue的生产者,Client又是这个Queue的消费者。
Queue代码:
import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class SafeQueue { private final Byte[] buffer; private int size; private int capacity; private int start; private int end; private ReentrantLock lock; private Condition notFull; private Condition notEmpty; private static SafeQueue queue; private SafeQueue(int size) { this.size = size; this.capacity = size; this.start = 0; this.end = 0; buffer = new Byte[size]; lock = new ReentrantLock(true); notFull = lock.newCondition(); notEmpty = lock.newCondition(); } public static SafeQueue getSingleInstance() { if (queue == null) { queue = new SafeQueue(20); for (int i = 0; i < 20; i++ ) { try { queue.put((byte)i); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return queue; } public void put(Byte newElement) throws InterruptedException { ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { try { while (capacity == 0) { notFull.await(); } } catch (InterruptedException e) { notFull.signal(); e.printStackTrace(); } buffer[end] = newElement; end = (end + 1) % this.size; capacity--; notEmpty.signal(); } finally{ lock.unlock(); } } public Byte peek() { ReentrantLock lock = this.lock; lock.lock(); try { try { if(capacity == size) { notEmpty.await(); } } catch (InterruptedException e) { notEmpty.signal(); } Byte result = null; result = buffer[start]; buffer[start] = null; start = (start + 1) % size; capacity++; notFull.signal(); return result; } finally { lock.unlock(); } } public byte[] peek(int length) { ReentrantLock lock = this.lock; lock.lock(); try{ byte[] result = new byte[length]; for(int i = 0; i < length; i++) { try { if(capacity == size) { notEmpty.await(); } } catch (InterruptedException e) { notEmpty.signal(); } result[i] = buffer[start]; buffer[start] = null; start = (start + 1) % size; capacity++; notFull.signal(); } return result; } finally{ lock.unlock(); } } public void put(byte[] elements, int length) throws InterruptedException { ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { for (int i = 0; i < length; i++) { try { while (capacity == 0) { notFull.await(); } } catch (InterruptedException e) { notFull.signal(); e.printStackTrace(); } buffer[end] = elements[i]; end = (end + 1) % this.size; capacity--; notEmpty.signal(); } } finally{ lock.unlock(); } } public int size() { ReentrantLock lock = this.lock; lock.lock(); try { return size - capacity; } finally{ lock.unlock(); } } public String toString() { String re = ""; for (int i = 0; i < 20; i++) { re+=buffer[i]+ "|"; } return re; }}
Server代码:
import java.io.BufferedInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Server extends Thread { ServerSocket server; /** * @param args */ //写个Socket发送数据 public Server() { try { server = new ServerSocket(9994); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void run() { Socket socket = null; while(true) { try { socket = server.accept(); executorService.execute(new DealThread(socket)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { // TODO Auto-generated method stub new Server().start(); }}class DealThread implements Runnable { Socket socket = null; SafeQueue queue = SafeQueue.getSingleInstance(); public DealThread(Socket socket) { this.socket = socket; } @Override public void run() { try { byte[] result = new byte[5]; System.out.println("New connection accepted "+socket.getInetAddress()+":"+socket.getPort()+ socket.getReceiveBufferSize()); BufferedInputStream reader = new BufferedInputStream(socket.getInputStream()); int length = reader.read(result, 0, result.length); while(length >= 0) { try { queue.put(result, length); System.out.println("server: " + queue); length = reader.read(result, 0, result.length); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated method stub } }
Client:
import java.io.BufferedOutputStream;import java.io.IOException;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Customer { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new Server().start(); ExecutorService executorService = Executors.newFixedThreadPool(10); for(int i = 0; i< 4; i++) { executorService.execute(new CustomerThread()); } }}class CustomerThread implements Runnable { Socket socket; SafeQueue queue = SafeQueue.getSingleInstance(); public CustomerThread() { try { socket = new Socket(InetAddress.getLocalHost(), 9994); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { while(true) { byte[] result = queue.peek(5); BufferedOutputStream writer = null; try { writer = new BufferedOutputStream(socket.getOutputStream()); writer.write(result); writer.flush(); System.out.println(queue); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
- JAVA : Socket和多线程
- java socket和多线程(Thread)实例
- java socket和多线程(Thread)实例
- java socket和多线程(Thread)实例
- Java socket和多线程(Thread)实例
- java socket和多线程(Thread)实例
- java Socket和ServerSocket多线程编程
- java socket多线程代码
- java多线程Socket例子
- java socket多线程聊天室
- Flex+Java Socket多线程
- Flex+Java Socket多线程
- Java多线程Socket资料
- Java Socket多线程
- java socket多线程聊天室
- java socket 多线程
- java socket 多线程
- java socket多线程编程
- 惊艳!9个不可思议的 HTML5 Canvas 应用试验
- 事务学习笔记(一)
- Spring中SSH的作用
- 实验1:small rtos51-------led控制
- IP地址和物理地址的区别,为什么要用这两种地址
- JAVA : Socket和多线程
- Maximum Subarrary
- hadoop集群添加删除节点
- cpu复位后怎么运行?
- 细说SQL Server中的加密
- VS2008编写的程序使用更低版本VS打开
- 【OpenGL】(NeHe教程学习)纹理映射及代码实现
- strcopy和strncopy区别
- phpcms v9 模板标签技巧,标签常用方法