This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!

David Crossett

Ranch Hand
+ Follow
since Feb 05, 2003
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 David Crossett

Yes. A multi-dimensional array is simply an array of arrays. Check out this code...it should help you figure out multi-dimensional arrays really quick if you run it!

As you can see, your arrays can be of varying lengths. Stare at the output of this program for awhile, then play around with it. It'll become clear after awhile. Hope this helps!
15 years ago
Thank you, Jim. Your example is perfect for then next question I was going to ask: How is it that you can call Thread.sleep() without using the threads actual name? For example, in this program I have 2 threads, thread: 0 and thread: 1. Because they both use the same run() method, is that why the call to generic class Thread.sleep() works? Would runner.sleep() and runner2.sleep() work also (assuming I declared the variables in a larger scope)? I've always wondered why I can just call Thread.WHATEVER and how that will affect the thread I'm running or even which one. Any comments?
OK...here's some code:

The output to this code is:


What is the most appropriate way to have the second thread yield() to the first thread, so my output is more split up between 0's and 1's? I'd like to see thread: 0 get a chance to execute even through thread: 1 has priority. Is this done in the run() method? Since both threads share the same run() method, how could I tell one thread to hold up while another executes? Would that be done with something like

...and if so, how would I handle manipulating multiple threads being spawned if I weren't planning on naming each one of them individually (say, using a for loop with an incrementing counter as the name)?
Or should the yield() be done in the constructor?
OK...thank you all...how about this one: I know that if I have a class that implements the Runnable interface, then I'm stating that I'm going to create a thread using "new Thread(this);" and define the run() method within that class. Do I still have one thread or two?
Just make sure you call super.paint(g) in your paint() method...that'll fix it.
15 years ago
What is your operating system? Some do time-slicing, some do round robin, some just run a thread until interrupted. What if you changed the priority of the thread that isn't getting a chance to run to a higher priority? Does that at least give you some CPU time? Also, make sure you're not implementing a thread's code in any sort of actionEvent or anything weird like that - code running in there doesn't give up control until it is complete or broken out of. Also, as a troubleshooting idea, can you manually interrupt the second thread to continue running the first one? As for socket or networking issues, I can't comment on that, sorry. I haven't gotten that far yet.
Am I to understand that, by default, new threads are in the main thread group. But what if I create a new Threadgroup...then would they be in THAT group instead? And I would have 3 thread groups running? (System, main, and whatever one I created?)
Thank you!
I'm trying to get this down absolutely correctly. I'm learning about threads and the different ways to implement them (Runnable vs. Thread class). My question is this: If I have a class, and IN that class I 'spin off' a new thread, then do I have two threads? Is the 'running' class considered an anonymous thread before the new thread I create begins execution? I just want to make sure. It seems to me that a Java program, since it's taking resources, etc., would by default be a thread - just an unnamed one. Any answers or insight?
Yes. Im sure there are 'deeper' answers than this, but the main reason I know of is this: Since you can only extends one thing ( single inheritance ), what happens if you want to create threads in a class that creates a JFrame (by extendsing JFrame) ? Well, this way you can implement "Runnable" (to create threads) and still extend whatever you want (for inheritance).
That should have worked. Here is something else you can try in the run() method -
do {
...whatever code you need done }
while ( cancelNotHit );
Then, in the actionPerformed method of the cancel button, have the cancel button flip the true/false status of cancelNotHit. Just an idea!
I believe I've seen the same thing as you have. In my example, I was missing the run() because I was using an applet. In that scenario, it seems, the start(), stop(), and run() methods of the applet over-ride the same methods of the Runnable interface ( I know stop() is deprecated...)
Not only for the above reasons, but this also:
In the life cycle of a thread, especially if you are dealing with multiple threads (like 3 or 4 ), Java handles these threads based on the states they are in. For example, ONLY after start() is called is a thread in the 'ready' state - and ONLY threads in the 'ready' state are in line for processor time. Once there is available processor time, the next 'ready' thread in line with the highest priority is allowed to execute its run() method, which puts that thread in a 'running' state. If this thread is interrupted for a higher priority thread, then it is told to wait(), and it is again put in the 'ready' state, which puts it back in line for processor time. I don't know WHO handles all of this, I just know this is HOW its handled. It would be interesting to know, however, if threads that have their run() method called directly actually run to completion regardless, or if they can be interrupted and put back in a ready state anyway. I'll have to write a tester program for that...maybe I'll post it.
Isn't the O.S. the one that decides how to use muliple processors? I didn't think there was a way to control that without writing seriously ugly O.S. level code or driver level code to control the hardware. My understanding is that we tell the computer what we would LIKE to have happen, and the O.S. controls exactly how that will be implemented. So maybe SOMETIMES you'll get multiple processors running your threads, and sometimes you'll only get access to one. This is just an OPINION coming from a Microsoft background - in other words, Microsoft doesn't implement multi-processors very well, so I'd be surprised if you could control access to them at all.
Without looking too deeply at your code, the first thing I see is that your loop will only run once. The reason is that - according to your 'if' conditional, the loop only runs AS LONG AS ALL letters == 0. Once you increment a letter ( which should be done B += 1 ), you make this statement false, so the loop stops executing. Does that help?
15 years ago
If Java (or programming) isn't an art, then any bozo could do it, and do it as well as any other bozo. Obviously, there are different levels of thinking that goes into developing software. It IS a science to make a computer respond to a user in a predictable way - simple cause and effect. However, to develop software that can grow and change over time (OOP), and to create solutions to complex problems using the limitations you're given (whether these limitations are called Java, C++, or VB) - THAT is an art. If you look at a painting, isn't it simply a canvas and some oil paints? Science created the tools, the artist created the Mona Lisa.
15 years ago