Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!

Phil Hopgood

Ranch Hand
+ Follow
since Jul 14, 2008
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Phil Hopgood

Ulf, Nitesh,

Thanks for that guys.

More stuff to play with

Regards,
Phil
Morning all,

In a multi-threaded application, is there any way of discovering/listing which threads (thread names) are still "alive", i.e. have not yet finished their run() method? I mean programmatically not using some monitor.

I know I can find out how many are active using Thread.activeCount() but is there an array or something lying around that holds the names for those threads?

I don't mean all the threads associated with the JVM's process of running the application, i.e. garbage collection, etc. just the ones related to the application, main, thread1, thread2, etc.

I'm guessing that there's no way of finding out the status of the active threads since by the time you've got that the status will probably have changed anyway.

Thanks for all your help, really appreciate it.
Regards,
Phil.
Ahh! Thanks Joanne.

I knew it had to be something like that and now you mention it I think I remember reading that somewhere

Regards,
Phil.
9 years ago
Hi All,

My query is about readLine() and when it returns null.

I've got a chat client/server (from the good ole Head First guys) and in the server code it sets up the following runnable job that's loaded into a thread:


A message is sent to the server every time the "Send" button on the GUI (not shown) is clicked and ends up in the run() method above.

What is confounding me is that once the message has been sent, picked up in the run() method and printed out, the while loop loops back to the readLine() and as there isn't a message straight away won't it return null?

I know this isn't the case because if it were the run() method would complete and the thread would die. This doesn't happen because I can type in and send another message any time and it gets printed out so the thread must still be running.

So the question is why isn't readLine() returning null if there's nothing to read? and since it clearly isn't returnng null when will it return null

I know this must be simple but it's not clear to me and I've looked through the doco and it hasn't helped me so far.

So I'd be glad of some help.

Thanks.
Regards,
Phil.
9 years ago
Thanks for that Ulf,

Yeah I thought that too but I just wanted to be sure that there wasn't some subtle underlying factor here that I was missing. Also I'm guessing that the reason why it was in the Head First book and thus not an issue is that the code was developed on a Mac and I'm assuming that Mac implements pnp differently - I'm using W2K and have no knowledge of Mac I'm afraid.

Thanks again,
Regards,
Phil.
Hi Everyone,

I'm a newbie to Java and to sockets, so, please excuse the idiot questions.

I should say now that I have solved this problem BUT I don't think I fully understand so I would be glad of your comments. The area of doubt relates specifically to port 5000.

I've been working through the Head First Java book and got to the bit where they do a simple chat server.

The server was created thus:


and the client socket:


The server started ok but when I fired up the client it wouldn't connect giving the following error message:



After a lot of checking of the code I decided to change the port to 4242 as I'd already got an even simpler example working using that port.

Great this worked ok. So I concluded that there was something else bound to port 5000. I had a look on the web and came up with this:


Port Authority Database

Port 5000

Name: upnp-evnt
Purpose: Universal Plug N' Play Event
Description: This TCP port is opened and used by Universal Plug N' Play (UPnP) devices to accept incoming connections from other UPnP devices. UPnP devices connect to each other using TCP protocol over port 5000.
Related Ports: 1900



Made sense to me. Latterly I did a search on JavaRanch to see if others had had the same problem with port 5000 and, although some people did, the solutions given didn't involve changing the port to something other than 5000.

So I'm wondering if I've missed something here.

Have I? What do you think?

Thanks in advance for ploughing through all this.

Regards,
Phil.
OK, this is getting spookily like my life, I start to think I understand it then...... wham! something happens and I'm confused again!

Arulk, thanks for your input. This now prompts me to ask a few more idiot questions just to clarify things, so I hope you can bare with me.

I understand the need for synchronization, in the old days we used to use semaphores to do this ..... a bit more clumsy than synchronization but the end result was similar. I understand about the granularity of synchronization.

What's confusing me at the present is this: as far as I can see a class and therefore each instance of that class, has only one lock. If within a class there is a piece of synchronized code, whether it's a block or a method, and a thread accesses that piece of code then that thread obtains a lock on ...... what?

Just the piece of synchronized code that the thread has entered?

or

The whole object, all it's code, including unsynchronized and, assuming that there are other unrelated synchronized bits of code, other synchronized bits of code?

or

Just the synchronized bits

Regards and thanks for all your help,
Phil.
Thanks for that Guys,

your answers make infinite sense...

I think I'll go play with threads for a bit now and try out the above scenario.

Regards,
Phil.
Afternoon all.

Suppose I have a runnable class that has methods A and B, say.

Method A is not synchronized in the method declaration but has some portion of code that is synchronized. This synchronized portion of code may be executed or it may not depending on the path taken through method A.

Method B is has no synchronization at all.

I declare one instance of this runnable job, DoSomethingClever, and start two threads, x and y with DoSomethingClever, i.e. the same job.

Now suppose thread x is running and enters the bit of synchronized code in method A and obtains a lock on DoSomethingClever and then gets sent to the runnable state.

Now thread y becomes runnable and needs to call method B, the one that's not synchronized. Will thread y have to wait until thread x has released it's hold on DoSomethingClever?

Supplementary Questions:

1. Can threads only access runnable objects? or is it just the thread's job that has to be runnable?

2. Can you assign a method or a portion of code of a method as being synchronized in an unrunnable class?

Idiot questions I'm sure but thanks for your help, can't tell you how helpful you all are ....

Regards,
Phil
Henry,

Aaaaaaah, that accounts for it! How silly of me.

Thanks for your prompt reply Henry.

Regards,
Phil.
9 years ago
I've been playing with (oops, learning about) threading and I've got the following code but it doesn't seem do what I expected it to:





Please ignore the cack-handed way of doing the time stamp I just wanted to see what was going on.

I get the following output:


27:30:296: In main, start myThread1
27:30:296: myThread1 sleeping for 10000
27:30:296: Starting Thread: 1
27:30:306: Starting Thread: 1 is sleeping for 2000
27:32:309: Thread: 1 awake now, Top o' the stack Ma!
27:40:301:back in main ...... am I?
27:40:301: Starting Thread: 2
27:40:301: Starting Thread: 2 is sleeping for 2000
27:42:303: Thread: 2 awake now, Top o' the stack Ma!



So what seems to happen is that main runs doing:
27:30:296: In main, start myThread1
27:30:296: myThread1 sleeping for 10000

Thread 1 then runs:
27:30:296: Starting Thread: 1
27:30:306: Starting Thread: 1 is sleeping for 2000

thread 1 sleeps for 2 secs:

27:32:309: Thread: 1 awake now, Top o' the stack Ma!

Main runs after another 8 secs making 10 in all:
27:40:301:back in main ...... am I?

then thread 2 runs with a 2 second sleep:
27:40:301: Starting Thread: 2
27:40:301: Starting Thread: 2 is sleeping for 2000
27:42:303: Thread: 2 awake now, Top o' the stack Ma!

So my questions are:
1. What effect does myThread1.sleep(10000); have? I expected thread 1 to sleep for 2 secs AND 10 secs the order being determined by when the jvm decided it should swap the threads about.

2. I put a big sleep in (sorry Raymond) to see if thread 2 or main would get a look in while thread 1 was asleep but this doesn't seem to be happening. So what is happening? Nothing happens while thread 1 is asleep. I'm confused.

I hope the above is clear.

Thanks for you time and help.
Regards,
Phil.
9 years ago
Oh no!!

I have just read a bit further through the book and they've gone through the server code that's needed to make this work a few pages later!

How embarrassing.

I guess I was too keen to get it coded and should have read the next few pages before getting going, what a larf ......

Regards,
Phil.
9 years ago
Amit,

That's a very good point!

I had just typed in the code from the book assuming it was connecting to some server or other. Of course "127.0.0.1" is local host, so no I don't have any server program running! What a twit I am.

In the preceding text of the book they use "190.165.1.103" so I tried that and it just times out. So either this was just an example that they didn't expect anyone to run, i.e. this "Advice Guy" service doesn't exist or it now no longer exists.

Thanks for your help.

Regards,
Phil.
9 years ago
Hi all,

I'm having trouble with the "DailyAdviceClient" example in Head First chapter 15.

I've typed in the example as shown below:



When I run this I get the following exception:

java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Metho
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.
at java.net.PlainSocketImpl.connectToAddress(PlainSock
at java.net.PlainSocketImpl.connect(PlainSocketImpl.ja
at java.net.SocksSocketImpl.connect(SocksSocketImpl.ja
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at DailyAdviceClient.go(DailyAdviceClient.java:9)
at DailyAdviceClient.main(DailyAdviceClient.java:26)



Obviously my socket connection has been refused.

Why is this?

Have I coded this incorrectly?

Is this server no longer valid? - after all the book was written a few years back.

I've had a quick Google and a search on JavaRanch and found no relevant references. I'd appreciate any inputs.

Regards and thanks.
Phil
9 years ago
Thanks Campbell, Ulf,

Really interesting stuff - how sad!

Especially about serialising/de-serialising using different versions. One to watch out for.

Thanks and regards,
Phil.
9 years ago