• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Implementing stack using threads

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am facing problems on how to implement stack push and pop operations using java multithreading...Please help
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 26458
81
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
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wanted to implement concurrency by using multithreading...If one thread push and other pops the element out of the stack.
 
Paul Clapham
Marshal
Posts: 26458
81
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

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to implement stack using threads but the program is not printing the popped elements..Please help



 
Paul Clapham
Marshal
Posts: 26458
81
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 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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh...So how to correct that?
 
gurkiran singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have used the same stack mystack[]
 
Marshal
Posts: 72407
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
All of the following truths are shameless lies. But what about this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic