Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #21697 > unrolled thread
| Started by | artik <olsztyn.arti@gmail.com> |
|---|---|
| First post | 2013-01-25 00:17 -0800 |
| Last post | 2013-01-25 10:11 -0800 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
Detection of socket connecting process, two thread problem artik <olsztyn.arti@gmail.com> - 2013-01-25 00:17 -0800
Re: Detection of socket connecting process, two thread problem Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> - 2013-01-25 00:40 -0800
Re: Detection of socket connecting process, two thread problem artik <olsztyn.arti@gmail.com> - 2013-01-25 02:33 -0800
Re: Detection of socket connecting process, two thread problem Knute Johnson <nospam@knutejohnson.com> - 2013-01-25 10:11 -0800
| From | artik <olsztyn.arti@gmail.com> |
|---|---|
| Date | 2013-01-25 00:17 -0800 |
| Subject | Detection of socket connecting process, two thread problem |
| Message-ID | <53a99ae7-6ff8-42d6-87f6-822a0310a475@googlegroups.com> |
Hi, I use one socket and two threads with it: one for reading and one for writing to it. My problem is in moment when error occurs and two thread want to do connection again on the same socket by closing the secont at start. Result of this is that while one one thread (the firtst of catching error) wants to start reconnecting the second one close socket to perform it for reconnecting. Classic dead lock. Therefore I ask about possibility to read state of socket exactly state of try to connecting? Regards, Artik
[toc] | [next] | [standalone]
| From | Peter Duniho <NpOeStPeAdM@NnOwSlPiAnMk.com> |
|---|---|
| Date | 2013-01-25 00:40 -0800 |
| Message-ID | <81jk5rtxpez7$.1vp8ezw6dkdfx$.dlg@40tude.net> |
| In reply to | #21697 |
On Fri, 25 Jan 2013 00:17:43 -0800 (PST), artik wrote: > Hi, > I use one socket and two threads with it: one for reading and one for > writing to it. My problem is in moment when error occurs and two thread > want to do connection again on the same socket by closing the secont at > start. Result of this is that while one one thread (the firtst of > catching error) wants to start reconnecting the second one close socket > to perform it for reconnecting. Classic dead lock. Therefore I ask about > possibility to read state of socket exactly state of try to connecting? It's only "classic dead lock" if you are using two different locks, acquiring them in a different order in the two different threads. You should only need one lock. And with only one lock, no deadlock. If that doesn't answer your question, post a proper code example, one that is concise, complete, and reliably reproduces the problem.
[toc] | [prev] | [next] | [standalone]
| From | artik <olsztyn.arti@gmail.com> |
|---|---|
| Date | 2013-01-25 02:33 -0800 |
| Message-ID | <743afe02-c761-461a-87ba-4c61f143bd0b@googlegroups.com> |
| In reply to | #21698 |
I'm newbie in Java and after several attempts and changing approaches i've decided to use synchronization like this below. I think it protects code (?) to use socket in the same time. Two threads using this procedures (disconnect and connect after) wait (if they need) when the second thread finished connecting and start to do the same: disconnect and connect. I can't find the way: don't doing disconnection-connection by the second thread when the first one started this just second before.
When error connections occurs in the threads (reading/writing) then call
reconnection procedure insede it are:
{...
close_connection();
set_connection();
...
}
which are below
public synchronized void close_connection() {
try {
socket.shutdownInput();
socket.shutdownOutput();
socket.close();
try {
Thread.sleep(500);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public synchronized boolean setconnection() {
boolean result=true;
socket = new Socket();
try {
socket.connect(new InetSocketAddress(address, port), 500);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(
socket.getOutputStream()));
} catch (IOException e)
{result=false;
}
return result;
}
Artik
[toc] | [prev] | [next] | [standalone]
| From | Knute Johnson <nospam@knutejohnson.com> |
|---|---|
| Date | 2013-01-25 10:11 -0800 |
| Message-ID | <kduhsm$cla$1@dont-email.me> |
| In reply to | #21703 |
On 1/25/2013 2:33 AM, artik wrote:
> I'm newbie in Java and after several attempts and changing approaches i've decided to use synchronization like this below. I think it protects code (?) to use socket in the same time. Two threads using this procedures (disconnect and connect after) wait (if they need) when the second thread finished connecting and start to do the same: disconnect and connect. I can't find the way: don't doing disconnection-connection by the second thread when the first one started this just second before.
>
> When error connections occurs in the threads (reading/writing) then call
> reconnection procedure insede it are:
> {...
> close_connection();
> set_connection();
> ...
> }
> which are below
>
> public synchronized void close_connection() {
> try {
> socket.shutdownInput();
> socket.shutdownOutput();
> socket.close();
> try {
> Thread.sleep(500);
> } catch (InterruptedException e1) {
> e1.printStackTrace();
> }
> } catch (IOException e1) {
> // TODO Auto-generated catch block
> e1.printStackTrace();
> }
> }
>
> public synchronized boolean setconnection() {
> boolean result=true;
> socket = new Socket();
> try {
> socket.connect(new InetSocketAddress(address, port), 500);
> in = new BufferedReader(new InputStreamReader(
> socket.getInputStream()));
> out = new BufferedWriter(new OutputStreamWriter(
> socket.getOutputStream()));
> } catch (IOException e)
> {result=false;
> }
> return result;
> }
>
> Artik
>
I like to keep this sort of thing very simple. Divide up the connection
handling part from the I/O part.
Main thread
Loop
Handle connection
Spawn service threads that handle I/O by passing socket
Wait until all service threads are finished
EndLoop
End Main thread
Service thread
Loop
Create readers and writers as appropriate
Do I/O and process
If error
Close socket to stop all service threads
Break out of loop
End Loop
End service thread
--
Knute Johnson
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web