The Big Programming Thread - Page 131
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
MisterD
Germany1338 Posts
| ||
berated-
United States1134 Posts
On April 05 2012 02:27 Millitron wrote: Read up on how your Linux JVM handles inner classes. I suspect its a linking issue though. Anyways, I got a question TL. I'm working on a fairly large project in Java, and I'm using Eclipse. My problem is that each version only changes a couple of methods in each class, so I like to collapse the ones that I'm not working on. But, since each version gets its own project, Eclipse seems to forget what methods I've collapsed, and displays them all, and its getting annoying to have to manually collapse them all every time I start a new version. Is there any way to make it remember this kind of thing between projects? I'm don't know of how to solve your problem as you describe it, but why does each version get its on project? Sounds like you need to be using some kind of version control to do your versioning, not eclipse. Edit: The reason I point this out is because if you did that, the current version you are working on could always stay in one project. Therefore, you wouldn't be switching projects constantly and losing your preferences. You would still be able to go back to previous version if you really needed to by checking out the previous version into a new project. Or you could see if this solution is still valid | ||
Millitron
United States2611 Posts
On April 05 2012 07:33 berated- wrote: I'm don't know of how to solve your problem as you describe it, but why does each version get its on project? Sounds like you need to be using some kind of version control to do your versioning, not eclipse. Edit: The reason I point this out is because if you did that, the current version you are working on could always stay in one project. Therefore, you wouldn't be switching projects constantly and losing your preferences. You would still be able to go back to previous version if you really needed to by checking out the previous version into a new project. Or you could see if this solution is still valid Well, its not a real, for-sale project. Its part of a course I'm taking. Each lab assignment adds a feature to the program made in the previous lab, so they all build off of each other. The thing is, they all get their own project so I can be sure I'm running the main method from the current version, and not a previous one. Anyways, the solution you suggested works well enough for me. Thanks! | ||
Shield
Bulgaria4824 Posts
Edit: I may give my code on PM if anyone wants to try to solve my problem. | ||
MisterD
Germany1338 Posts
On April 05 2012 20:09 darkness wrote: Well, I have 2 inner classes within an outer class, but only the last one isn't executed. I'm starting to hate Java on Linux. :/ Edit: I may give my code on PM if anyone wants to try to solve my problem. what do you even mean by "executing a class"? the only way to "execute" a class is to use it as main class which will execute it's main method, but you can't just execute a class itself and you can only always have one main class, not multiple. Try to explain your problem properly and if you have no clue how to do that, pm me your stuff and maybe i can then understand what you mean Oo | ||
Shield
Bulgaria4824 Posts
On April 05 2012 20:56 MisterD wrote: what do you even mean by "executing a class"? the only way to "execute" a class is to use it as main class which will execute it's main method, but you can't just execute a class itself and you can only always have one main class, not multiple. Try to explain your problem properly and if you have no clue how to do that, pm me your stuff and maybe i can then understand what you mean Oo I meant that both inner classes are called in main(), but the 2nd one doesn't start for some reason. I get no errors. | ||
GaiaCaT
35 Posts
On April 05 2012 23:18 darkness wrote: I meant that both inner classes are called in main(), but the 2nd one doesn't start for some reason. I get no errors. Could you post your code/pseudocode, or upload it to pastebin.com? I am still not sure what you mean by calling inner classes. You don't call them, you create an instance of inner classes, either through an instance of the outer class: + Show Spoiler + outerClass myOuterClass = new outerClass(); Or through the constructors or methods of outerClass: + Show Spoiler + outerClass myOuterClass = new outerClass(); | ||
MisterD
Germany1338 Posts
On April 05 2012 23:18 darkness wrote: I meant that both inner classes are called in main(), but the 2nd one doesn't start for some reason. I get no errors. have you done something like this? class OuterClass{ Because if that's the case, i assume your issue is this: Your main methods contain endless loops or at least run for a long time. Therefore, only one of them will seem to start initially, because the call InnerClass2.main(); will only be executed after InnerClass1.main(); _finished_. If you want to run them parallel, you will have to use threads. but then again, just shooting blind because you don't post any actual information on what you are doing. And now i'm done with shooting blind, so if you want further replies, post your stuff. | ||
Shield
Bulgaria4824 Posts
Anyway, I've fixed my problem. A value wasn't properly updated only on Linux, so it didn't go through if {}. Here is a bit of code to show you what I mean: + Show Spoiler +
TLDR: value changes were unknown to the 2nd (inner) class, it was always zero for it. However, that problem existed only on Linux jdk 1.6. Windows and FreeBSD proved to be fine. P.S. I'm aware there are some syntax and identation errors, but you get the point. | ||
GaiaCaT
35 Posts
On April 06 2012 01:45 darkness wrote: However, that problem existed only on Linux jdk 1.6. Windows and FreeBSD proved to be fine. I don't think this is a bug. You need to synchronize reads/gets too, otherwise you're not guaranteed to retrieve the most recent value. The reason Windows and FreeBSD seems to work is likely because they update value "fast enough" for the 2nd thread to pick up, unlike Linux which is still using a cached value. If the first thread finishes its job too fast and causes the second thread to terminate, the second thread might never get access to an updated value. This would explain why your if-block never fires unless you call getValue() before the if-block. Might want to use this instead: if (getValue() >= 1) { } If 2nd thread only ever reads value, you could also consider just declaring value as volatile which should ensure any reads will return the most recent write. | ||
CecilSunkure
United States2829 Posts
![]() | ||
Shield
Bulgaria4824 Posts
On April 06 2012 03:26 GaiaCaT wrote: I don't think this is a bug. You need to synchronize reads/gets too, otherwise you're not guaranteed to retrieve the most recent value. The reason Windows and FreeBSD seems to work is likely because they update value "fast enough" for the 2nd thread to pick up, unlike Linux which is still using a cached value. If the first thread finishes its job too fast and causes the second thread to terminate, the second thread might never get access to an updated value. This would explain why your if-block never fires unless you call getValue() before the if-block. Might want to use this instead: if (getValue() >= 1) { } If 2nd thread only ever reads value, you could also consider just declaring value as volatile which should ensure any reads will return the most recent write. It sounds logical. Btw, a friend of mine has installed Ubuntu as virtual machine and there was no problem. Could we say it's also up to JDK and/or kernel version? | ||
GaiaCaT
35 Posts
But as far as I know, it could be architecture, kernel, JVM, and maybe even CPU load and core-dependant. The current implementation does not enforce any synchronization of variables accessed through unsynchronized reads. That means a JVM could as example keep a variable cached for 10 seconds and it would still be within standards because you're supposed to read from within a synchronized method/block or declare the variable as volatile if you want the most up-to-date value. | ||
tec27
United States3690 Posts
On April 06 2012 08:51 darkness wrote: It sounds logical. Btw, a friend of mine has installed Ubuntu as virtual machine and there was no problem. Could we say it's also up to JDK and/or kernel version? Its a race condition, so the output is indeterminant on any architecture. The reason calling getValue() fixes it is because its a synchronized method, which forces the thread to lock until the object is available (not being used in another synchronized method call). Say you have thread 1 increment the value, and thread 2 output it, the events are essentially: Thread 1
Thread 2
Now, since you're running those actions in separate threads, its completely up to the OS scheduler as to when they will take place. Assuming a starting value of 5, they might interleave like: T1 - Retrieve [local = value = 5] T1 - Increment [local = 6] T1 - Store [value = 6] T2 - Retrieve [local = value = 6] T2 - Output [6] (this is an okay/desired outcome) Or like: T1 - Retrieve [local = value = 5] T1 - Increment [local = 6] T2 - Retrieve [local = value = 5] T1 - Store [value = 6] T2 - Output [5] (this is one of the ways to get to your 'buggy' state) There are a bunch more possible orderings, but I think you get the point ![]() ![]() T1 - Retrieve (OBJECT LOCKED) [local = value = 5] T2 - Retrieve (object is locked, thread blocks until object is available) T1 - Increment [local = 6] T1 - Store (OBJECT UNLOCKED) [value = 6] T2 - Continues with Retrieve [local = value = 6] T2 - Output [6] But as you can see, this ordering still depends on what thing gets the lock first. You can do some things to make sure they lock in the right order, but this problem is also better solved by just not threading ![]() | ||
![]()
MrHoon
![]()
10183 Posts
![]() | ||
Chaosvuistje
Netherlands2581 Posts
On April 06 2012 13:03 MrHoon wrote: man i wish i can trade my korean language for programming ![]() I'd say that programming languages are a fair bit easier to understand than the syntax of Korean. Then again, it all depends on where you are born. But really, syntax is much easier for computer languages than regular languages. | ||
IMlemon
Lithuania296 Posts
| ||
Weson
Iceland1032 Posts
| ||
alwinuz
Netherlands77 Posts
On April 07 2012 09:40 Weson wrote: Anyone know a good site or anything where you can learn ASP.NET with C#? Just started a class and we're working in Visual Studio Web Developer. I got a book but it's more of a reference book rather than a learning book. What part of ASP.net are you focusing on? For example, I think MVC is much easier than Web Forms. There's lots of training articles and videos at the official site: http://www.asp.net/mvc | ||
Ultraliskhero
Canada249 Posts
I'm doing file i/o using C on a linux environment, and I am wondering if there is a way to delete the contents of an entire file that was opened earlier without closing the file. Basicly, I first opened an existing file using the "r+" mode for fopen(), then I'm reading the file until EOF, and then I want to delete the contents of the file and write to it again starting from the begining. However, I cannot simply close the file and reopen it using the "w" mode for fopen() because I have the file locked using flock(), and I do not want to close (unlock) the file before I finish doing all the operations on it. Thanks. | ||
| ||