多线程的方法介绍与使用

wait函数 wait函数会使得到监视器锁的共享对象从运行状态变为挂起,同时会释放这把锁,但是不会释放当前线程其他共享对象上的锁。 以下几个事件可以打断被wait阻塞挂起的状态 ...

April 26, 2022 · 5 min

进程与线程

进程与线程的含义 进程: 进程是代码在数据集合上的一次运行活动 ,是系统进行资源分配和调度的基本单位,一个进程可以有多个线程。 线程: 进程中的一个执行任务(控制单元),负责当前进程中程序的执行。一个进程至少有一个线程,一个进程可以运行多个线程,多个线程可共享数据。 ...

April 26, 2022 · 4 min

reactor模型了解

reactor模型 特点 事件驱动 可以处理一个或多个输入源 通过多路复用将请求的事件分发给对应的处理器处理 根据大神Doug Lea 在 《Scalable IO in Java 》中的介绍,Reacotr模型主要分为三个角色 ...

February 10, 2022 · 3 min

netty利用websocket协议开发服务端

介绍websocket协议 WebSocket是HTML5开始提供的一种浏览器与服务器间进行全双工通信的网络技术,WebSocket通信协议于2011年被IETF定为 标准RFC6455,WebSocket API被W3C定为标准。 ...

February 9, 2022 · 4 min

使用netty作为http文件服务

代码 HttpFileServer package http; import echo.server.EchoServer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.http.*; import io.netty.handler.stream.ChunkedWriteHandler; /** * @author ggBall * @version 1.0.0 * @ClassName HttpFileServer.java * @Description TODO * @createTime 2022年02月08日 09:41:00 */ public class HttpFileServer { private static final String default_url = "/src/main/java/http/file/netty/"; public void run(final int port , final String url) throws InterruptedException { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(boss,worker) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { /** * This method will be called once the {@link Channel} was registered. After the method returns this instance * will be removed from the {@link ChannelPipeline} of the {@link Channel}. * * @param ch the {@link Channel} which was registered. * @throws Exception is thrown if an error occurs. In that case the {@link Channel} will be closed. */ protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); // 请求消息解码器 ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));// 目的是将多个消息转换为单一的request或者response对象 ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());//响应解码器 ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());//目的是支持异步大文件传输() ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));// 业务逻辑 } }); ChannelFuture future = bootstrap.bind("127.0.0.1", port).sync(); System.out.println("http文件服务器,地址:http://127.0.0.1:" + port + url); future.channel().closeFuture().sync(); } finally { boss.shutdownGracefully(); worker.shutdownGracefully(); } } public static void main(String[] args) throws Exception { int port = 8080; if (args.length > 0) { try { port = Integer.parseInt(args[0]); } catch (NumberFormatException e) { e.printStackTrace(); } } String url = default_url; if (args.length > 1) url = args[1]; new HttpFileServer().run(port, url); } } HttpFileServerHandler ...

February 8, 2022 · 3 min
总访问量 总访客数