aspose file tools*
The moose likes Beginning Java and the fly likes Implementing stack using threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implementing stack using threads" Watch "Implementing stack using threads" New topic
Author

Implementing stack using threads

gurkiran singh
Greenhorn

Joined: Feb 02, 2012
Posts: 6
I am facing problems on how to implement stack push and pop operations using java multithreading...Please help
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8207
    
  23

gurkiran singh wrote:I am facing problems on how to implement stack push and pop operations using java multithreading...Please help

Have a look at the synchronized keyword in the Java tutorials.

If you have any more specific questions, please elaborate.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If you want help, you need to TellTheDetails(⇐click) and ShowSomeEffort.(⇐click) It's impossible for anybody to help you if you don't provide details about what you tried and what particular problems you encountered.
gurkiran singh
Greenhorn

Joined: Feb 02, 2012
Posts: 6
I am confused where to start. Should I make 2 threads, one carrying out the push operation and other pop. If anybody could help me with the structure.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Not really, no. Why would you want more than one thread accessing the stack?

Which is really what you have to explain. You just said you want to implement push and pop operations with multiple threads, but you didn't say why. For me, since there's no particular reason to use more than one thread, I would say your first step should be to explain why you want to do that, or what you hope to achieve by doing it.
gurkiran singh
Greenhorn

Joined: Feb 02, 2012
Posts: 6
I wanted to implement concurrency by using multithreading...If one thread push and other pops the element out of the stack.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

gurkiran singh wrote:I wanted to implement concurrency by using multithreading...If one thread push and other pops the element out of the stack.


Then that appears to answer your question about whether you should use two threads... yes, that requirement does use two threads. Did you have another question?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

gurkiran singh wrote:I wanted to implement concurrency by using multithreading...If one thread push and other pops the element out of the stack.


Why? Is this an academic exercise to help you learn about multithreading? If so, google for something like producer consumer example. Most of the examples you'll see will probably use a FIFO, while you're asking about a LIFO, but that makes no difference as far as the multithreading is concerned.

Also, it's not likely that the stack implementation itself will use threads. The stack shouldn't care how many threads are accessing it. You'll just implement a stack, or use an existing one, and then write some multithreaded code that uses it (as per the producer/consumer examples you have by now studied thoroughly).

If you really do want your stack to be "implemented using threads", you'll have to provide a clear, precise explanation of what exactly you mean by that.

Finally, if you don't know where to start, read this: http://home.earthlink.net/~patricia_shanahan/beginner.html
gurkiran singh
Greenhorn

Joined: Feb 02, 2012
Posts: 6
I am trying to implement stack using threads but the program is not printing the popped elements..Please help



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

I put the "code" tags around your code, so it would look more readable. (It does look more readable, don't you think so?)

Anyway, were you thinking that your "stackpush" object was using the same stack as your "stackpop" object? It isn't. The two objects use different stacks, so the "stackpush" object pushes 10 numbers onto its stack and the "stackpop" object does nothing because its stack is always empty.
gurkiran singh
Greenhorn

Joined: Feb 02, 2012
Posts: 6
Oh...So how to correct that?
gurkiran singh
Greenhorn

Joined: Feb 02, 2012
Posts: 6
i have used the same stack mystack[]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Have you got your stack working in a single-threaded environment? Get that working first.
Why have you got an array of stacks?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

gurkiran singh wrote:I am trying to implement stack using threads


Sigh...

Once again: It's not likely that the stack implementation itself will use threads. The stack shouldn't care how many threads are accessing it. You'll just implement a stack, or use an existing one, and then write some multithreaded code that uses it (as per the producer/consumer examples you have by now studied thoroughly).

If you really do want your stack to be "implemented using threads", you'll have to provide a clear, precise explanation of what exactly you mean by that.

When somebody points out that something is unclear, merely repeating that thing doesn't magically clear it up.

So, what exactly do you mean by "implement stack using threads"? Does your stack class itself have to make use of multiple threads? If so, how? Or do you mean, as I suggested, that you have to just implement a stack class, and then use that stack in a multithreaded context?

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Implementing stack using threads