由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 新手问一个多线程的问题
相关主题
新手求教 BufferedReader.readLine()请问一下
新手问题Re: How to showStatus in Applet?
can applet implements runnable?Re: connection pool
问一个blocking IO的程序ZT: 关于性爱的多线程问题研究(一)
ThreadLocal 的一个 use case问个Thread 的问题,java certificate里的
Timer and TimerTask●●●●紧急求助JAVA初级问题,今天project due●●●●
傻问题,关于java里的资源释放能这么 create thread 吗?
请问关于用threadPoolExecutor实现threadpool的问题?implements runable 和 extends thread
相关话题的讨论汇总
话题: client话题: socket话题: private话题: file
进入Java版参与讨论
1 (共1页)
m*****y
发帖数: 224
1
我要实现一个简单的web server,不需要考虑过载之类太复杂的情况,就假定有20个左
右的clients会同时连接上来。需要对于每一个进来的client都分一个thread去处理。
我做了一个clientHandler class
public class ClientHandler implements Runnable{
private Socket imcoming; //client
private BufferredReader reader; //read from client socket
private PrintWriter writer; //write to client socket
public void clientInterpreter(){
//处理client的各种请求
}

public void run(){
clientInterpreter();
}

private int methodA(String filePath){
File afile = new File(file
q*********u
发帖数: 280
2
要不要sychronized关键字和是不是implements runnable是两个问题,不要扯到一起去。
照你这个handler, 所有的handler thread如果有同时update/write某个相同资源的时
候,再用synchronized
具体入门代码查java network programming
Buffered
receiver -> mission queue -> worker thread in worker(handler) pool
|
^this must be syned

我要实现一个简单的web server,不需要考虑过载之类太复杂的情况,就假定有20个左
右的clients会同时连接上来。需要对于每一个进来的client都分一个thread去处理。
我做了一个clientHandler class
public class ClientHandler implements Runnable{
private Socket imcoming; //c

【在 m*****y 的大作中提到】
: 我要实现一个简单的web server,不需要考虑过载之类太复杂的情况,就假定有20个左
: 右的clients会同时连接上来。需要对于每一个进来的client都分一个thread去处理。
: 我做了一个clientHandler class
: public class ClientHandler implements Runnable{
: private Socket imcoming; //client
: private BufferredReader reader; //read from client socket
: private PrintWriter writer; //write to client socket
: public void clientInterpreter(){
: //处理client的各种请求
: }

m*****y
发帖数: 224
3
哦谢谢!
我的server是这样写的:
public class MyAppServer {
private static final int PORT = 8000;
public static void main(String[] args) {
try {
ServerSocket socket = new ServerSocket(PORT);
try {
while (true) {
final Socket connection = socket.accept();
Runnable task = new ClientHandler(connection);
new Thread(task).start();
}
} catch (IOException e1) {
System.err.println("Error while accepting connection on port " + PORT);
} finally {


【在 q*********u 的大作中提到】
: 要不要sychronized关键字和是不是implements runnable是两个问题,不要扯到一起去。
: 照你这个handler, 所有的handler thread如果有同时update/write某个相同资源的时
: 候,再用synchronized
: 具体入门代码查java network programming
: Buffered
: receiver -> mission queue -> worker thread in worker(handler) pool
: |
: ^this must be syned
:
: 我要实现一个简单的web server,不需要考虑过载之类太复杂的情况,就假定有20个左

g*****g
发帖数: 34805
4
For your case, this is fine. Normally you could have
potentially unlimited concurrent users, and you want to
use a threadpool, which could 1, limit the number of
threads, 2, reuse threads.
That's when the concurrency kicks in.

【在 m*****y 的大作中提到】
: 哦谢谢!
: 我的server是这样写的:
: public class MyAppServer {
: private static final int PORT = 8000;
: public static void main(String[] args) {
: try {
: ServerSocket socket = new ServerSocket(PORT);
: try {
: while (true) {
: final Socket connection = socket.accept();

q*********u
发帖数: 280
5
这个server是典型的教科书的初级例子,如果你的要求就是几个client的话,应该就可
以了。
深入下去的话,东西就多了,比如workers thread pool和connection queue, 还有
workers的数量,这个讲究大了,不是拍拍脑袋定的
socket这个东西里面可以设置的buffer有好几个,可以优化.

【在 m*****y 的大作中提到】
: 哦谢谢!
: 我的server是这样写的:
: public class MyAppServer {
: private static final int PORT = 8000;
: public static void main(String[] args) {
: try {
: ServerSocket socket = new ServerSocket(PORT);
: try {
: while (true) {
: final Socket connection = socket.accept();

m*****y
发帖数: 224
6
多谢各位耐心指点。我刚刚接触多线程,看了一堆书,实践的还不多。
对于我这个例子,各位前辈看看我说的对不对:
首先,每当有client要连接我的server的时候,server都会创建一个新的
clientHandler 的Object,然后交给一个thread去运行。假如现在有5个client连进来
,就会有5个clientHandler objects被创建出来,所以clientHandler的那三个private
members: reader, writer, connection就没有concurrency的问题。
然而,这5个clientHandler对本地文件进行操作的话,就会出现concurrency的情况,
要避免出现问题,就要给那个private int methodA(String filePath)方法加上
synchronized关键字。
另外还有一句题外话,我这么设计是不是不太合理?是不是应该内存中就只出现一个
clientHandler object去处理这5个client连接?其它的要用到thread pool的情况,我
也在书本上看到了,现在暂时还没遇到

【在 q*********u 的大作中提到】
: 这个server是典型的教科书的初级例子,如果你的要求就是几个client的话,应该就可
: 以了。
: 深入下去的话,东西就多了,比如workers thread pool和connection queue, 还有
: workers的数量,这个讲究大了,不是拍拍脑袋定的
: socket这个东西里面可以设置的buffer有好几个,可以优化.

q*********u
发帖数: 280
7
Tuning the pool size
Tuning the size of a thread pool is largely a matter of avoiding two
mistakes: having too few threads or too many threads. Fortunately, for most
applications the middle ground between too few and too many is fairly wide.
Recall that there are two primary advantages to using threading in
applications: allowing processing to continue while waiting for slow
operations such as I/O, and exploiting the availability of multiple
processors. In a compute-bound application running on

【在 m*****y 的大作中提到】
: 多谢各位耐心指点。我刚刚接触多线程,看了一堆书,实践的还不多。
: 对于我这个例子,各位前辈看看我说的对不对:
: 首先,每当有client要连接我的server的时候,server都会创建一个新的
: clientHandler 的Object,然后交给一个thread去运行。假如现在有5个client连进来
: ,就会有5个clientHandler objects被创建出来,所以clientHandler的那三个private
: members: reader, writer, connection就没有concurrency的问题。
: 然而,这5个clientHandler对本地文件进行操作的话,就会出现concurrency的情况,
: 要避免出现问题,就要给那个private int methodA(String filePath)方法加上
: synchronized关键字。
: 另外还有一句题外话,我这么设计是不是不太合理?是不是应该内存中就只出现一个

g*****g
发帖数: 34805
8
You have to synchronize on the class lock in your case,
or your can use FileLock

private

【在 m*****y 的大作中提到】
: 多谢各位耐心指点。我刚刚接触多线程,看了一堆书,实践的还不多。
: 对于我这个例子,各位前辈看看我说的对不对:
: 首先,每当有client要连接我的server的时候,server都会创建一个新的
: clientHandler 的Object,然后交给一个thread去运行。假如现在有5个client连进来
: ,就会有5个clientHandler objects被创建出来,所以clientHandler的那三个private
: members: reader, writer, connection就没有concurrency的问题。
: 然而,这5个clientHandler对本地文件进行操作的话,就会出现concurrency的情况,
: 要避免出现问题,就要给那个private int methodA(String filePath)方法加上
: synchronized关键字。
: 另外还有一句题外话,我这么设计是不是不太合理?是不是应该内存中就只出现一个

m*****y
发帖数: 224
9
高手,咱们先别说thread pool了,先说前面简单的东西我理解的对不对吧,谢谢了。

most
machine
threads
Indeed

【在 q*********u 的大作中提到】
: Tuning the pool size
: Tuning the size of a thread pool is largely a matter of avoiding two
: mistakes: having too few threads or too many threads. Fortunately, for most
: applications the middle ground between too few and too many is fairly wide.
: Recall that there are two primary advantages to using threading in
: applications: allowing processing to continue while waiting for slow
: operations such as I/O, and exploiting the availability of multiple
: processors. In a compute-bound application running on

m*****y
发帖数: 224
10
哦,你的意思是说这样的么,,比如那个 methodA
public int methodsA(String filePath){
syncronized(this){
//file manipulation here...
}
}

【在 g*****g 的大作中提到】
: You have to synchronize on the class lock in your case,
: or your can use FileLock
:
: private

相关主题
Timer and TimerTask请问一下
傻问题,关于java里的资源释放Re: How to showStatus in Applet?
请问关于用threadPoolExecutor实现threadpool的问题?Re: connection pool
进入Java版参与讨论
g*****g
发帖数: 34805
11
No, this is not class lock. You have to sync on blahblah.class

【在 m*****y 的大作中提到】
: 哦,你的意思是说这样的么,,比如那个 methodA
: public int methodsA(String filePath){
: syncronized(this){
: //file manipulation here...
: }
: }

m*****y
发帖数: 224
12
晕了,给个example?谢谢

【在 g*****g 的大作中提到】
: No, this is not class lock. You have to sync on blahblah.class
m*****y
发帖数: 224
13
能不能问一下,这种情况为什么一定要class lock?
你的意思是说methodA应该写成
public static syncronized int MethodA(String filePath){
//file operations..
}
这样的吧?

【在 g*****g 的大作中提到】
: No, this is not class lock. You have to sync on blahblah.class
g*****g
发帖数: 34805
14
Because they are different objects, this "this" is different from
that "this"

【在 m*****y 的大作中提到】
: 能不能问一下,这种情况为什么一定要class lock?
: 你的意思是说methodA应该写成
: public static syncronized int MethodA(String filePath){
: //file operations..
: }
: 这样的吧?

m*****y
发帖数: 224
15
明白了,多谢!

【在 g*****g 的大作中提到】
: Because they are different objects, this "this" is different from
: that "this"

m******t
发帖数: 2416
16
If we are worried by possible race conditions on the files being
manipulated, just use file level locks. There is no point synchronizing
in the code as far as I can see.
m*****y
发帖数: 224
17
file locks跟操作系统有关吧?

【在 m******t 的大作中提到】
: If we are worried by possible race conditions on the files being
: manipulated, just use file level locks. There is no point synchronizing
: in the code as far as I can see.

m******t
发帖数: 2416
18

There is a FileLock API.

【在 m*****y 的大作中提到】
: file locks跟操作系统有关吧?
1 (共1页)
进入Java版参与讨论
相关主题
implements runable 和 extends threadThreadLocal 的一个 use case
几个问题Timer and TimerTask
一个基本问题。傻问题,关于java里的资源释放
关于inner class的继承请问关于用threadPoolExecutor实现threadpool的问题?
新手求教 BufferedReader.readLine()请问一下
新手问题Re: How to showStatus in Applet?
can applet implements runnable?Re: connection pool
问一个blocking IO的程序ZT: 关于性爱的多线程问题研究(一)
相关话题的讨论汇总
话题: client话题: socket话题: private话题: file