|
pearofducks posted:Quick concurrency question (I'm new to threads). I've got a homemade generic linked list, most of the operations on it take place inside a loop, and only one operation actually uses the list. So I'm wondering if its possible to have the linked list operation run in its own thread, and the loop to keep going. I've read up on it some and it seems like it just provides the ability to override the run method, but I'd like to have any of the linked list actions be in their own thread. Is this possible? If so can someone just give me a brief idea of what to do? Why'd you roll your own linked list when the JDK provides you with one (java.util.LinkedList) that has built-in concurrency support (using the Collections class) already? That said, you probably could do what you want to do but you're being pretty vague about what "operations" and "actions" you need to perform. Can you be a little more precise?
|
# ? Sep 28, 2008 20:56 |
|
|
# ? Apr 28, 2024 12:34 |
|
pearofducks posted:Quick concurrency question (I'm new to threads). I've got a homemade generic linked list, most of the operations on it take place inside a loop, and only one operation actually uses the list. So I'm wondering if its possible to have the linked list operation run in its own thread, and the loop to keep going. I've read up on it some and it seems like it just provides the ability to override the run method, but I'd like to have any of the linked list actions be in their own thread. Is this possible? If so can someone just give me a brief idea of what to do? It's certainly possible, and if you really just want to know how to allocate a thread to perform a task which only accesses immutable data and doesn't need to communicate with the rest of the program, I'd be happy to tell you. However, that doesn't sound like the sort of thing you have in mind, and so I feel obliged to warn you that concurrency is an extremely complex and difficult programming area with many, many pitfalls to it, and it is not really feasible to explain it in the scope of a forum thread. Suffice it to say that just naively allocating threads for incidental tasks will almost certainly not make your program faster.
|
# ? Sep 28, 2008 21:11 |
|
This is true. Honestly, if the idea is to naively create threads for the above reasons, then you're probably using the wrong data structure. Work on your algorithm and work with your data, then once you've gotten there, work on concurrency and optimization.
|
# ? Sep 28, 2008 21:19 |
|
Sorry for the lack of clarity before. I was using this as more of a learning exercise, hence why I rolled my own linked list and the desire to make it concurrent. What I'm confused by is how I'd make both remove and add (which both need to traverse the list to remove at an index) threaded. Like I said before, with my current limited knowledge it seems like you can only implement run, so I don't know how to bridge the gap. There are two areas of this project I wanted to see implement this setup. Initial list population: Could be importing unsorted data, but then populating the list sorted, so it will be using add(index, element), and should have plenty of time before the list is actually used. Element removal: Whenever an element is removed (again at an index), a loop continues to run, but doesn't need the list again for enough time that the element should be able to be removed. \\vvvvv awesome, thanks for the help guys pearofducks fucked around with this message at 00:33 on Sep 29, 2008 |
# ? Sep 28, 2008 21:24 |
|
Okay. First off, the thread has nothing to do with your data structure. There's no way you can just say "aha, all the operations on this data structure will magically happen in a different thread"; you'll have to change the implementation of each operation to create a thread to do the work. So the add method will look something like: code:
|
# ? Sep 28, 2008 22:00 |
|
Without putting to fine a point on it, you will have tremendous problems if you think that you'll have "enough time" to modify a list before some other thread can access it. To answer your question, check out the synchronized keyword and the Collections.synchronizedList() method, if you implement List then that will basically solve it for you. Also, there are a few books on Java concurrency that are pretty good: specifically Java Concurrency in Practice and Concurrent Programming in Java: Design Principles and Patterns if you are interested. Concurrency is frighteningly difficult to do correctly, especially with some of Java's more quirky semantics and rules involving synchronization.
|
# ? Sep 28, 2008 22:01 |
|
I've been learning to write code in BlueJ, but it's poo poo. What IDE should I get instead?
|
# ? Sep 29, 2008 15:48 |
|
Boz0r posted:I've been learning to write code in BlueJ, but it's poo poo. What IDE should I get instead? Didn't know that was still out, used that back when I took my first programming class in college (used C++ during HS, Clemson taught Java). Eclipse and Netbeans are the two best free ones. Lot of people like IntelliJ IDEA, never used it but you have to pay for it. Fore the most part I develop with Eclipse but have been playing around with Netbeans 6.5 Beta because of the improved webservices and groovy support.
|
# ? Sep 29, 2008 16:42 |
|
Hey guys. I'm having a slight problem with this simple java program i'm writing for my class. Here is my code: http://pastebin.com/m71a64ee4 I'm pretty sure I got everything working, except for some reason my methods increaseSalaryByPercent() and decreaseSalaryByPercent() methods arn't really doing anything; they're just returning the same salary that was it's argument. This is my first program in java, so please don't bash me too hard if it turns out it's an easy answer. Also, did you guys ever have a teacher who forced you to code in a certain style? My Java teacher is forcing us to use Allman indenting (which I use anyway so it doesn't really matter), and having us put those large comments before each method or class. I was surprised that he is making us do this; I thought that an important aspect of programming was being able to code in your own style.
|
# ? Sep 29, 2008 19:25 |
|
Sarah Sherman posted:Hey guys. I'm having a slight problem with this simple java program i'm writing for my class. Here is my code: You need to use this.name and this.salary to change those variables in your increase/decrease methods.
|
# ? Sep 29, 2008 19:45 |
|
sonic bed head posted:You need to use this.name and this.salary to change those variables in your increase/decrease methods. Thought that at first, but it actually is a problem with integer math. He is passing a into into the increase/decrease functions then dividing by 100. Sarah Sherman: in the declartion, change it from "int percent" to "double percent" to fix it. In any language that supports types you get to have fun with integer math. You think (Percent/100) will return .1 or whatever because you are taking decimal points for granted. Because Percent is declared as an integer Java will treat the product of the division function as a integer as well and that means chomping off the decimal point. So .5 becomes 0, 1.9 becomes 1, ect. Either make percent floating point or cast it to double by placing (double) in front of it. code:
|
# ? Sep 29, 2008 20:02 |
|
^^Ah, this seemed to have fixed it. Thanks!sonic bed head posted:You need to use this.name and this.salary to change those variables in your increase/decrease methods. should I change the code to look like this?: code:
lmao zebong fucked around with this message at 20:04 on Sep 29, 2008 |
# ? Sep 29, 2008 20:02 |
|
Sarah Sherman posted:^^Ah, this seemed to have fixed it. Thanks! The keyword 'this' tells java you mean to change the object's global variable. If you use the same variable name in a method Java assumes the the method variable is not the same as the global. For example from some code I am looking at: code:
For more detail: http://java.sun.com/docs/books/tutorial/java/javaOO/thiskey.html
|
# ? Sep 29, 2008 20:11 |
|
dvinnen posted:The keyword 'this' tells java you mean to change the object's global variable. If you use the same variable name in a method Java assumes the the method variable is not the same as the global. For example from some code I am looking at: This might explain why changing salary to this.salary doesn't do anything in Sarah Sherman's example, but why it would do something in the BinaryFileMessage example.
|
# ? Sep 29, 2008 20:48 |
|
Sarah Sherman posted:Also, did you guys ever have a teacher who forced you to code in a certain style? My Java teacher is forcing us to use Allman indenting (which I use anyway so it doesn't really matter), and having us put those large comments before each method or class. I was surprised that he is making us do this; I thought that an important aspect of programming was being able to code in your own style. Yep, in one of my earlier classes we could lose up to 20% per assignment for incorrect formatting, and every class except the earliest couple would dock marks for missing or poor javadoc.
|
# ? Sep 30, 2008 00:16 |
|
triplekungfu posted:Yep, in one of my earlier classes we could lose up to 20% per assignment for incorrect formatting, and every class except the earliest couple would dock marks for missing or poor javadoc. I had a drat S/390 Assembler professor knock of 67% of an assignment because of lack of comments. That might sound acceptable until I mention that the uncommented lines were printlines. Goddamn that guy.
|
# ? Sep 30, 2008 00:42 |
|
Hm, somehow missed this earlier.Sarah Sherman posted:Also, did you guys ever have a teacher who forced you to code in a certain style? My Java teacher is forcing us to use Allman indenting (which I use anyway so it doesn't really matter), and having us put those large comments before each method or class. I was surprised that he is making us do this; I thought that an important aspect of programming was being able to code in your own style.
|
# ? Sep 30, 2008 10:15 |
|
Incoherence posted:Hm, somehow missed this earlier. My only counter argument to this, how often do you go back and look at your own code from semesters past?
|
# ? Sep 30, 2008 13:11 |
|
Does anyone know if there is some sort of cool algorithm for cutting up a random convex/concave polygon into triangles in a smart way, or if it's impossible to do automatically?
|
# ? Sep 30, 2008 13:58 |
|
MEAT TREAT posted:My only counter argument to this, how often do you go back and look at your own code from semesters past? These things don't really pay off while you're still at uni. But they're very important skills once in the workforce.
|
# ? Sep 30, 2008 15:42 |
|
Boz0r posted:Does anyone know if there is some sort of cool algorithm for cutting up a random convex/concave polygon into triangles in a smart way, or if it's impossible to do automatically? You're thinking of polygon triangulation, Siedel's Algorithm handles this.
|
# ? Sep 30, 2008 22:25 |
|
I have a quick question can anyone look at my code and tell me why it's printing 1, 5, 3, 2 when it should be printing 5 4 3 2 1. I'm trying to emulate a stack using two queues. Thanks. code:
|
# ? Oct 1, 2008 17:57 |
|
I suspect that part of your problem is that you're checking q1.size() and q2.size() every time through the loop, even though you're also decreasing the size of the queue, so you only get halfway through before the loop ends. Also, you only put four pop() statements at the end, not five.
|
# ? Oct 1, 2008 19:15 |
|
Edit: Dumb poo poo, should pay attention what I'm doing. You were doing it right. Also what Incoherence said, that is why you are only getting 4 elements back I think. lamentable dustman fucked around with this message at 19:55 on Oct 1, 2008 |
# ? Oct 1, 2008 19:33 |
|
Incoherence posted:I suspect that part of your problem is that you're checking q1.size() and q2.size() every time through the loop, even though you're also decreasing the size of the queue, so you only get halfway through before the loop ends. That was it. I changed the q1.size() to while(q1.peek() != null). Thanks for the help.
|
# ? Oct 1, 2008 20:02 |
|
Edit: got it.
icehewk fucked around with this message at 00:07 on Oct 5, 2008 |
# ? Oct 4, 2008 23:51 |
|
Goons I ask for your help with a simple program, not exactly sure what im doing wrong this is the practice for my homework for java class I keep getting errors no matter what I do. //This is a program to tell simple intrtest //Written by //october 5th, 2008 //JDK 1.6 import javax.swing.*; import java.text.*; public class Chp4prac1 { public static void main (String [ ] args) { String response; response = JOptionPane.showInputDialog(null, "Dollars you would like to borrow"); double Dollars = Double.parseDouble(response); { String response; response = JOptionPane.showInputDialog(null, "Interest is"); double Interest = Double.parseDouble(response); String response; { response = JOptionPane.showInputDialog(null, "years"); double years = Double.parseDouble(response); Interest over time=Dollars*Interest*years; { }// ends main method }// ends program At first I thought it was suppose to be Int for the year and interest but I'm having no luck at all. the end always pops an error usually line 24 or 25 saying missing } even though its there. For a simple interest program I'm so confused. if against the rules to post practice work i apologize in advance. i'm just utterly confused.
|
# ? Oct 6, 2008 00:06 |
|
The third-last line has a { where there should be a }, for starters. Also you seem to have pasted the same bit of code over itself a few times?
|
# ? Oct 6, 2008 00:39 |
|
zootm posted:The third-last line has a { where there should be a }, for starters. Also you seem to have pasted the same bit of code over itself a few times? I was under the impression that copying the same style of a line would also pop the Joptionpane up for the interest rate and years to be entered?
|
# ? Oct 6, 2008 00:52 |
|
craig8429 posted:snip code:
|
# ? Oct 6, 2008 02:13 |
|
Jethro posted:Put your code in [code ][ /code] blocks, and use proper indentation, like thus: That actually does help a fair bit. I just noticed that I completely messed up the panes.I fixed it all however it has lead to another issue, when I do the formula Dollars*years*rate/100 it refuses to show it in the pane like i need it. I'm a play with it a bit more and see if i can get it. Thank you so far by the way everyone I appreciate the tips. Trying to learn to program from a book isn't as easy as i had hoped. Edit here is the redone version. code:
craig8429 fucked around with this message at 02:47 on Oct 6, 2008 |
# ? Oct 6, 2008 02:38 |
|
code:
Second of all your indenting is bizzare. Take another look at "code blocks" we don't put { } just for the hell of it.
|
# ? Oct 6, 2008 22:43 |
|
Never mind, thought that was a quote!
|
# ? Oct 6, 2008 23:02 |
|
Is there a platform independent way to open a text file using the default system text editor? Barring that, how difficult would it be to set up some swing components to display text files? I have very little experience with GUI programming or Swing.
|
# ? Oct 7, 2008 03:14 |
|
Pesch posted:Is there a platform independent way to open a text file using the default system text editor? java.awt.Desktop supports browse(), edit(), mail(), open() and print(). The Swing component you're looking for is called JTextArea. The simplest way to put data into it would be to use the default backing Document and then repeatedly append() lines from a file to the end. That works up to several megabytes. If the file's too big for that, Document does support alternative backing methods like just mapping a file into memory and using that as the Document model. That's probably far more difficult than most people would care to implement.
|
# ? Oct 7, 2008 03:46 |
|
there is a recommendation in one of my assignment guidelines that i use the java debugger "jdb" but i can't figure out where it is. it's definitely not in my jre/bin directory where java.exe is, but everything i find on it online says "just type jdb instead of java at the command line" and whenever I do that i get the not recognized command alert. i'm using winXP. can anyone help me get jdb running? i'd appreciate it a ton.
|
# ? Oct 11, 2008 04:55 |
|
Rabbi Dan posted:there is a recommendation in one of my assignment guidelines that i use the java debugger "jdb" but i can't figure out where it is. jdb should install in the same place as java and javac. If it didn't then you didn't get the JDK and you only got the JRE or some other poo poo that isn't the JDK.
|
# ? Oct 11, 2008 06:19 |
|
It's been about a year since I've coded in anything and I'm looking for a book or something that could give me a good Java refresher and then go into some OOP. If there's something out there that I could read that would give me both of those and perhaps start out on some game development that would be great. Any ideas would be very much appreciated.
|
# ? Oct 13, 2008 23:31 |
|
I have a jsp with a menu bar that is being <jsp:included> into many other pages. The menu bar is simple, just a list with "Home", "Join", "Contact" and other things like that. I want to know what the best way is to underline the name of the current page without splitting up the jsp that holds the menu, in case something needs to change. I am using struts in case that changes anything. I know that I can send a parameter using the include, but I don't know how best to use that parameter to control "text-decoration:underline." Thanks.
|
# ? Oct 16, 2008 15:04 |
|
|
# ? Apr 28, 2024 12:34 |
|
sonic bed head posted:I know that I can send a parameter using the include, but I don't know how best to use that parameter to control "text-decoration:underline." Thanks. Your best bet would probably be to add a css class of some sort.
|
# ? Oct 16, 2008 23:37 |