Java review-basic5
1. How would you write a socket client/server in Java
The server DateServer.java
package edu.lmu.cs.networking; import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date; /**
* A TCP server that runs on port 9090. When a client connects, it
* sends the client the current date and time, then closes the
* connection with that client. Arguably just about the simplest
* server you can write.
*/
public class DateServer { /**
* Runs the server.
*/
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
try {
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
} finally {
socket.close();
}
}
}
finally {
listener.close();
}
}
}
The Client side:
package edu.lmu.cs.networking; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket; import javax.swing.JOptionPane; /**
* Trivial client for the date server.
*/
public class DateClient { /**
* Runs the client as an application. First it displays a dialog
* box asking for the IP address or hostname of a host running
* the date server, then connects to it and displays the date that
* it serves.
*/
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
Socket s = new Socket(serverAddress, 9090);
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
2. Main Differences Betwen Java NIO and IO:
IO NIO
Stream oriented Buffer oriented
Blocking IO Non blocking IO
Selectors
3. Stream Oriented vs. Buffer Oriented
The first big difference between Java NIO and IO is that IO is stream oriented, where NIO is buffer oriented. So, what does that mean?
Java IO being stream oriented means that you read one or more bytes at a time, from a stream. What you do with the read bytes is up to you. They are not cached anywhere. Furthermore, you cannot move forth and back in the data in a stream. If you need to move forth and back in the data read from a stream, you will need to cache it in a buffer first.
Java NIO's buffer oriented approach is slightly different. Data is read into a buffer from which it is later processed. You can move forth and back in the buffer as you need to. This gives you a bit more flexibility during processing. However, you also need to check if the buffer contains all the data you need in order to fully process it. And, you need to make sure that when reading more data into the buffer, you do not overwrite data in the buffer you have not yet processed.
个人理解,这是I/O和NIO最主要的区别:
1)IO是stream-oriented 主要在于读入无法缓存,不能向前或者向前或者向后访问
2)NIO是buffer-oriented读入缓存在buffer中,可以向前或者向后进行访问
4. Blocking vs. Non-blocking IO
Java IO's various streams are blocking. That means, that when a thread invokes a read() or write(), that thread is blocked until there is some data to read, or the data is fully written. The thread can do nothing else in the meantime.
Java NIO's non-blocking mode enables a thread to request reading data from a channel, and only get what is currently available, or nothing at all, if no data is currently available. Rather than remain blocked until data becomes available for reading, the thread can go on with something else.
The same is true for non-blocking writing. A thread can request that some data be written to a channel, but not wait for it to be fully written. The thread can then go on and do something else in the mean time. What threads spend their idle time on when not blocked in IO calls, is usually performing IO on other channels in the meantime. That is, a single thread can now manage multiple channels of input and output.
个人理解,这也是IO与NIO的区别
1)Blocking 当stream在读写一个资源的时候其他事不能访问,直到读写操作完成
2)利用Chanel对资源的可用部分进行读写,并不影响资源的其他部分,其他部分可以继续其他的读写操作。
5. socket multiplexing:
Multiplexing is running multiple connections over one socket, all messages for those connections will be received on that socket (or send). So it's not two-way communication, but multiple different communication channels that are handled by one socket.
6. Sleep vs wait
Sleep:1 It is a static method syntax: Thread.sleep(). It makes the current thread into not running state for specified time in milli seconds.
2 During sleep, the thread never release lock.
Wait:1 It is an Object level method. It makes the current thread into not running state.
2 During wait, the thread can be waken by notify() or notifyAll() method in synchronized context.
7. Process vs thread
Thread: A thread is a single sequential flow of control within a program.
Process: A process has self-contained execution environment. A process generally has a complete,private set of basic
run-time resources.
There are two types of multitasking: process-based and thread-based.
Difference:
Both processes and threads are independent sequences of execution.
1 Threads runs in a shared memory space while processes run in a separate memory spaces.
2 A process can contain more than one thread. So a process is considered as "heavyweight" while a thread is deemed as "lightweight".
3 Process are heavily dependent on system resources while threads require minimal amounts of resource.
4 Modifying a main thread may affect subsequent threads while changes on a parent process will not necessarily affect child processes.
5 Threads within a process coomunicate directly while the process do not communicate so easily.
6 Threads are easy to create while processes are not that straightforward.
8. Life cycle of a Thread (Thread States)
A thread can be in one of the five states. According to sun, there is only 4 states in thread life cycle in java new, runnable, non-runnable and terminated. There is no running state.
But for better understanding the threads, we are explaining it in the 5 states.
The life cycle of the thread in java is controlled by JVM. The java thread states are as follows:
1.New
2.Runnable
3.Running
4.Non-Runnable (Blocked)
5.Terminated

Java review-basic5的更多相关文章
- Java Review (一、Java开发环境)
@ 目录 Java程序运行机制 高级语言运行机制 编译型语言 解释型语言 Java运行机制和JVM 编写 编译 运行 Java开发工具包 JDK JRE JDK.JRE与JVM HelloWord 编 ...
- java:Review(Oracle-HTML-CSS)
20170708_review: 1.oracle: 对表的操作: 使用命令行建立一张表:create table 表名 (列名 列名的类型 primarty key, ....); alter ta ...
- [Java 教程 01] Hello,Java!
前言 从事编程已经有一段时间了,突然发现,Java作为我的第一编程语言,自己似乎对她并有一个系统的思想.当下Java依旧保持着超高的热度,新特性也不断出现,从当初学习的java6版本到最近刚出的jav ...
- Java Algorithm Problems
Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...
- [Java 教程 00] 计算机基础
前言 我想,来到这的朋友肯定是想学习JAVA或者想要进入IT这个行业的.考虑到大家的基础可能不一样,有些人可能还是用着新买的电脑,为了让大家在后续的学习中更加顺畅.在学习一门全新的计算机语言之前,我需 ...
- [Java 教程 04] Java基础语法
在上一篇文章中我们已经运行了个简单的java程序,但是没有给大家讲解代码部分的内容与含义.学习,我们要做到知其然而知其所以然,所以本篇文章我们就来讲解java程序的基本语法,学完这篇文章你再回头看上篇 ...
- [Java 教程 03] 我的第一个Java程序
现在,大家应该都已经安装好jdk环境了吧!是不是已经跃跃欲试,按耐不住心中的小激动了?那我们现在就来写我们java学习生涯中的第一个java程序. 文件相关设置 为了方便后面大家的学习呢?有一点大家还 ...
- [Java 教程 02] 开发环境搭建
在上一篇文章对Java做了一个简单介绍之后,我想大家都已经对她有一个初步的认识了吧!那踏入正式学习使用Java之前,我们有一步是不得不做的,它是什么呢?没有错,就是我们本篇文章的标题所说,搭建Java ...
- Java时间格式字符串与Date的相互转化
目录 将Date转化为格式化字符串 时间格式字符串转化为Date @ 将Date转化为格式化字符串 将Date转化为格式化字符串是利用SimpleDateFormat类继承自 java.text.Da ...
- 一些常见JAVA问题
原文:https://blog.csdn.net/weiyongxuan/article/details/45920765 一.Java的异常的基类是java.lang.Throwable 二.守护线 ...
随机推荐
- loj2290 随机二分图
题意:有一个左右各n个点的二分图,对于连边组有一些性质:1号组的一条边,有50%的概率出现.2号组两条边,有50%的概率同时出现,50%的概率同时不出现.3号组两条边,有50%的概率出现第一条,有50 ...
- 洛谷P4027 [NOI2007]货币兑换
P4027 [NOI2007]货币兑换 算法:dp+斜率优化 题面十分冗长,题意大概是有一种金券每天价值会有变化,你可以在某些时间点买入或卖出所有的金券,问最大收益 根据题意,很容易列出朴素的状态转移 ...
- 牛客网在线判题系统JavaScript(V8)使用
JavaScript作为一种弱类型的编程语言,语法和C/C++.JAVA等存在差别,但是对于大部算法题,不只是C/C++.JAVA,也依然可以使用JavaScript来实现.所以在牛客网中,如果你喜欢 ...
- 杂项-公司:Amazon
ylbtech-杂项-公司:Amazon 亚马逊公司(Amazon,简称亚马逊:NASDAQ:AMZN),是美国最大的一家网络电子商务公司,位于华盛顿州的西雅图.是网络上最早开始经营电子商务的公司之一 ...
- Charles抓包(http/https请求)
Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装官网下载安装Charles:https://www.charlesproxy.com/download/当然由于国情可以使用破 ...
- 从三层架构到Spring框架
首先是软件的应用分层架构 标准三层架构: 1:数据访问层:实现了数据的持久化 2:业务逻辑层:对逻辑的实现及处理,实际上不可能在表示层对数据不做任何处理,但是尽可能的将逻辑分为一层 3:表示层:数据的 ...
- CSS奇数、偶数、指定数样式
原文: https://blog.csdn.net/wangjia200913/article/details/49615325 语法 :nth-child(an+b) 第一种:简单数字序号写法 ...
- reac-native + typescript 的环境搭建
一. RN-TS环境搭建 . 安装RN脚手架 yarn add create-react-native-app -g yarn global add typescript . 创建项目文件夹 crea ...
- PostgreSQL DISTINCT ON
https://stackoverflow.com/questions/3800551/select-first-row-in-each-group-by-group select DISTINCT ...
- [转]WPF的Presenter(ContentPresenter)
这是2年前写了一篇文章 http://www.cnblogs.com/Clingingboy/archive/2008/07/03/wpfcustomcontrolpart-1.html 我们先来看M ...