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的更多相关文章

  1. Java Review (一、Java开发环境)

    @ 目录 Java程序运行机制 高级语言运行机制 编译型语言 解释型语言 Java运行机制和JVM 编写 编译 运行 Java开发工具包 JDK JRE JDK.JRE与JVM HelloWord 编 ...

  2. java:Review(Oracle-HTML-CSS)

    20170708_review: 1.oracle: 对表的操作: 使用命令行建立一张表:create table 表名 (列名 列名的类型 primarty key, ....); alter ta ...

  3. [Java 教程 01] Hello,Java!

    前言 从事编程已经有一段时间了,突然发现,Java作为我的第一编程语言,自己似乎对她并有一个系统的思想.当下Java依旧保持着超高的热度,新特性也不断出现,从当初学习的java6版本到最近刚出的jav ...

  4. Java Algorithm Problems

    Java Algorithm Problems 程序员的一天 从开始这个Github已经有将近两年时间, 很高兴这个repo可以帮到有需要的人. 我一直认为, 知识本身是无价的, 因此每逢闲暇, 我就 ...

  5. [Java 教程 00] 计算机基础

    前言 我想,来到这的朋友肯定是想学习JAVA或者想要进入IT这个行业的.考虑到大家的基础可能不一样,有些人可能还是用着新买的电脑,为了让大家在后续的学习中更加顺畅.在学习一门全新的计算机语言之前,我需 ...

  6. [Java 教程 04] Java基础语法

    在上一篇文章中我们已经运行了个简单的java程序,但是没有给大家讲解代码部分的内容与含义.学习,我们要做到知其然而知其所以然,所以本篇文章我们就来讲解java程序的基本语法,学完这篇文章你再回头看上篇 ...

  7. [Java 教程 03] 我的第一个Java程序

    现在,大家应该都已经安装好jdk环境了吧!是不是已经跃跃欲试,按耐不住心中的小激动了?那我们现在就来写我们java学习生涯中的第一个java程序. 文件相关设置 为了方便后面大家的学习呢?有一点大家还 ...

  8. [Java 教程 02] 开发环境搭建

    在上一篇文章对Java做了一个简单介绍之后,我想大家都已经对她有一个初步的认识了吧!那踏入正式学习使用Java之前,我们有一步是不得不做的,它是什么呢?没有错,就是我们本篇文章的标题所说,搭建Java ...

  9. Java时间格式字符串与Date的相互转化

    目录 将Date转化为格式化字符串 时间格式字符串转化为Date @ 将Date转化为格式化字符串 将Date转化为格式化字符串是利用SimpleDateFormat类继承自 java.text.Da ...

  10. 一些常见JAVA问题

    原文:https://blog.csdn.net/weiyongxuan/article/details/45920765 一.Java的异常的基类是java.lang.Throwable 二.守护线 ...

随机推荐

  1. 为什么不直接使用socket ,还要定义一个新的websocket 的呢

    大致概念: TCP/IP 协议,是网络七层协议的第四层,本身没有长连接或短连接的区别: HTTP 是基于 TCP 协议之上的「短连接」应用层协议,它的出现极大简化了网络应用的实现门槛,丰富了应用: S ...

  2. css3 ---2 属性的选择器

    存在和值属性选择器1:[attr]:该选择器选择包含 attr 属性的所有元素,不论 attr 的值为何. [name]{ background: pink; } <!DOCTYPE html& ...

  3. 网络编程(client发信息给server)

    client发信息给server

  4. 洛谷P5338 [TJOI2019]甲苯先生的滚榜

    原题链接洛谷P5338 [TJOI2019]甲苯先生的滚榜 题目描述 甲苯先生在制作一个online judge,他发现做比赛的人们很关心自己的排名(显而易见),在acm赛制的比赛中,如果通过题目数量 ...

  5. EF 中获取 TableAttribute的值,即数据库中真实的表名

    比如EF中我定义了这样一个实体: [Table(Name = "MyTableName")] public class MyClass { } [Table(Name = &quo ...

  6. mac下企业邮件不能发送的问题

    1,选用服务器:smtp.example.qq.com     使用ssl      用密码      端口:465

  7. 如何正确使用 Flink Connector?

    本文主要分享 Flink connector 相关内容,分为以下三个部分的内容:第一部分会首先介绍一下 Flink Connector 有哪些.第二部分会重点介绍在生产环境中经常使用的 kafka c ...

  8. js检测到如果是手机端就跳转到手机端的网址代码

    if((/AppleWebKit.*Mobile/i.test(navigator.userAgent)||/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcat ...

  9. 如何用js造轮子

    写了一个非常通俗易懂的造轮子的方法 <div class="wrap"></div> <div class="wrap">& ...

  10. Intelij Idea 2016破解

    在注册时选择License server,输入http://www.iteblog.com/idea/key.php,点击OK