Slyck.com
 
Slyck Chatbox - And More

At all LW developers (and all other Java programmers)

A place for developers and programmers of file-sharing software to discuss issues.
Forum rules
PLEASE READ BEFORE POSTING: Slyck Forum Rules

At all LW developers (and all other Java programmers)

Postby no_dammagE » Mon Aug 01, 2005 2:58 pm

Hi,

I have several questions regarding the JRE and the language.

Well, a move off pascal to java is not always easy. The first problem is deployment since the JRE is 15 megs big. Some things are different, but in most cases the language itself is the same (for old-style programming).

My questions are targeting java's (in my case Sun's distribution) threading and networking since those were the things I never really used due to the fact that I always had to develop client-side delphi applications not accessing streams with high latency.
So Borland's IDE actually did all the work for me and IF I needed some networking IO, it also always was low latency so that blocking never appeared.

Now I want to develop with Java and although the Javadocs are in most cases good, I'll need some proper documentation regarding threading and asynchronous IO (NIO?) I don't know if I lack google skills or does google lack search ability, but I couldn't find any proper documentation regarding both. For NIO the problem is that all the tutorials I read either don't really describe anything or describe everything, but not really enough. For threading until now I couldn't find good descriptions regarding thread communication and especially queues.

If you know any links to good tutorials regarding both, please submit. If not, don't bother to google - the things I'll find myself :)

--

Regarding the JRE:
JIT and Hotspot(in Suns JRE) optimize the code on runtime and compile it into native code. Since JIT doesn't compile everything, but only critical parts, I have a question: is there an ability to enforce code compilation of parts I want to be compiled? If not, that's not a problem.
A second thing is if I nullpoint an object, will its allocated memory freed again? e.g. CObj o=new CObj(); o=null;

Or let's do it different: of course the garbage collector will kick all the objects spawned inside a function as soon as it finished/returned. But if I have global variables inside a class and at one point I won't need them (and I really know that). Will nullpointing help the GC to free the object? In bigger projects it might come to situation that the GC can't distinguish if the object is still needed or not...And since the VM never has enough RAM, I think that it might be a better idea (although it could sometimes lead to no->exceptions...)

Well, that's all I think.
/me is off watching ETTV
Windows? Blah. Linux? Blah. BSD? Blah.
Just make sure you have a computer licence and I can open your fsckin files.
Vorbis | Theora | LaTeX | OpenDocument
User avatar
no_dammagE
 
Posts: 652
Joined: Sat Jul 05, 2003 9:37 am

Postby gregorio » Mon Aug 01, 2005 5:35 pm

This is a decent tutorial for NIO
http://www.cs.brown.edu/courses/cs161/p ... io-ltr.pdf

I don't know what you mean by thread communication.

Assigning a null to a variable will not force the previously associated object to be garbage collected but if you remove all references to an object that way, you will allow it to be garbage collected. It's much simpler to define variables that point to objects only in the block where they are needed, so they are automatically garbage collected when you exit the block.

I don't think there is a way to force the JIT to compile a certain part, why would you require something like that?
LimeWire 4.13 is out. Don't miss it!
User avatar
gregorio
 
Posts: 309
Joined: Fri Jul 04, 2003 7:01 am

Postby no_dammagE » Tue Aug 02, 2005 6:56 am

Regarding JITC enforcement I was just curious.

Thanks for the link to NIO communication. I overflew it and I'm pretty sure that that is what I'll need.

Under thread communication I meant thread accessing to variables or methods of other threads. Yes, all threads share the same memory space allowing the threads to access the same variables. A problem raises if two threads access one variable at the same time. So I have to lock it. That's not a really big problem, the bigger problem is:

I have for example in Thread A a class running in a loop waiting for orders, once Thread B is done with his job, it has to call a callback function in Thread A. That callback function has to be ran inside the A and not B thread. Until now I came to only one single idea of doing this - declare a public variable in class A which is inited as null and A's mainloop checks the variable's state. Once B is done, it submits the results into A's variable, the mainloop notices that and kicks the needed function.

The problem is that most of the time the A thread is doing nothing except eating resources due to the fact that it checks variable's state.

A more elegant way would be to have a queue thread containing params for threads of the same class running and spawned threads of that class check the queue if there is job to be done. E.g. in LimeWire it would be that 2-3 threads get file paths into the queue and hashing threads can pop it out of the queue and process (hashing, metadata and so on). But I'm sure LW did it better ;) That's only theory, I never really programmed threads. Of course if I'll pervert myself, I'll do communication between threads via sockets on the mainloop. *lol*

But regarding threading I think I was successful in my search now.

[EDIT]
A small question raised after a smoke regarding memory allocation: ah, although it is not important anymore, once the queue has data, a thread can just init an instance of the needed class to parse data :)
Windows? Blah. Linux? Blah. BSD? Blah.
Just make sure you have a computer licence and I can open your fsckin files.
Vorbis | Theora | LaTeX | OpenDocument
User avatar
no_dammagE
 
Posts: 652
Joined: Sat Jul 05, 2003 9:37 am

Postby gregorio » Wed Aug 03, 2005 6:16 pm

Java provides very simple methods for locking on objects etc. If you have multiple threads accessing and modifying the same variable, you would write synchronized accessors for this variable:
Code: Select all
public class SomeClass {
     private Object variable;
     public synchronized void setVariable(Object newValue) {
          variable = newValue;
     }

     public synchronized Object getVariable() {
         return variable;
     }
}


the synchronize keyword obtains a lock on the parent class.
As an alternative you can also use
Code: Select all
synchronize (someObjectToLockOn) {
...
}

to lock on different objects.
LimeWire 4.13 is out. Don't miss it!
User avatar
gregorio
 
Posts: 309
Joined: Fri Jul 04, 2003 7:01 am

Postby gregorio » Wed Aug 03, 2005 6:19 pm

A useful queue that uses one thread to handle tasks can be found here:

http://www.limewire.org/fisheye/viewrep ... Queue.java
LimeWire 4.13 is out. Don't miss it!
User avatar
gregorio
 
Posts: 309
Joined: Fri Jul 04, 2003 7:01 am

Postby no_dammagE » Sat Aug 06, 2005 6:45 pm

thank you gregorio, that was very helpful
Windows? Blah. Linux? Blah. BSD? Blah.
Just make sure you have a computer licence and I can open your fsckin files.
Vorbis | Theora | LaTeX | OpenDocument
User avatar
no_dammagE
 
Posts: 652
Joined: Sat Jul 05, 2003 9:37 am


Return to Developers Forum

Who is online

Users browsing this forum: No registered users and 1 guest

© 2001-2008 Slyck.com