• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Waiting loop

 
Greenhorn
Posts: 5
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need a waiting loop.
I tried something like this:
for (int i = 0; i < 100; i++) {
System.out.print("");
}

but I think it's not the best solution.

I can't use either Object.wait() or Thread.sleep(), because I just need to delay a thread so that all work is not executed just by one thread (counting to 100, one thread can perform it very quickly thus other threads don't get a chance).
It has to be a loop, but this:
for (int i = 0; i < 100; i++) {}
doesn't work because JVM considers it useless and doesn't execute it at all.

I hope you get the point at least, my English vocabulary is not good when I speak about java staff etc..
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vaclav. Welcome to The Ranch!

What makes you think Thread.sleep() doesn't do what you want? That's what it's for - making a thread wait without consuming any resources.
 
Sheriff
Posts: 28333
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually no, I don't get your point. What exactly are you trying to do?

And when I ask that question, I'm not asking about that business about loops. I'm asking about this:

I need a waiting loop.



What's a "waiting loop"? And if you had one of them, what would you be using it for? In other words, what is the problem to which this "waiting loop" is the answer?
 
Vaclav Dedik
Greenhorn
Posts: 5
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:Hi Vaclav. Welcome to The Ranch!

What makes you think Thread.sleep() doesn't do what you want? That's what it's for - making a thread wait without consuming any resources.


Hi,
I don't exactly know how to put this, it might not be comprehensible.. but.. I was told that it masks/covers (not sure which word to use :/) mistakes in synchronization. It's like, you don't realize whether you did a mistake in synchronization or you didn't when using Thread.sleep() (some of them).
 
Vaclav Dedik
Greenhorn
Posts: 5
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Actually no, I don't get your point. What exactly are you trying to do?

And when I ask that question, I'm not asking about that business about loops. I'm asking about this:

I need a waiting loop.



What's a "waiting loop"? And if you had one of them, what would you be using it for? In other words, what is the problem to which this "waiting loop" is the answer?



I was worried whether it was the right expression :)

Well, I mean a piece of a code that causes a delay in thread execution. An example:

When two or more threads execute this, the first thread executes the first iteration but because it is fast, it executes even the second iteration, the third and so on. The second thread doesn't get a chance because the first thread is too fast.
In this case, the input looks like this:
Thread-0 writes number: 0
Thread-0 writes number: 1
Thread-0 writes number: 2
Thread-0 writes number: 3
.
.
.

So, I can add a piece of a code that delays the first thread so that the second thread can execute the following iteration:

The input looks like this:
Thread-0 writes number: 0
Thread-1 writes number: 1
Thread-0 writes number: 2
Thread-1 writes number: 3
.
.
.

This solves the problem but it just doesn't look like the best solution, there has to be something better.

I hope it is more comprehensible :|
 
Paul Clapham
Sheriff
Posts: 28333
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still can't imagine why you would need to have several threads count to 100, but at any rate the way to delay a thread is to have it call the Thread.sleep() method.
 
Vaclav Dedik
Greenhorn
Posts: 5
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I still can't imagine why you would need to have several threads count to 100, but at any rate the way to delay a thread is to have it call the Thread.sleep() method.


It's just for educational purposes. Maybe I should've mentioned this earlier...
 
Saloon Keeper
Posts: 15731
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just use Thread.sleep(). It does exactly what you're asking.
 
Vaclav Dedik
Greenhorn
Posts: 5
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trust me that I would if I could.. But I can't.. This is my homework and if I use Thread.sleep(), I won't get any points.
It's just a simple program and you can imagine that the loop of mine (for block) represents a part of the program and it does something else (something useful but quite demanding).
 
Paul Clapham
Sheriff
Posts: 28333
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So that brings us back to an earlier question... what's the point of all this? If you want to have your thread pause, you use Thread.sleep. If you have some homework assignment which says you can't do that, then possibly there's a reason for that. Or possibly the homework assignment is just stupid. But in any case we have no idea of what the point is, since you haven't posted much useful information.
 
I do some of my very best work in water. Like this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic