• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using "Static" in Threads, Bad Practice?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using "Static" in Threads,Bad Practice?
"This is a question very dealt in forums. I personally don't like the "static" declaration, since it implies in one hand that you can use methods from a class without the need to declare objects from the very class, and in the other hand the direct sharing of attributes for all the instances of a class.
Both behaviours are, in my opinion, a bad practice of OOP, but allowed in JAVA (like a lot of other things).
" My teacher.

What do you think about the "static" declaration?

Apart from the opinions that anyone might have with this declaration, I am in this situation:
If you have dealt with Concurrent Java you might know that it's very common to use the example of Producer-Consumer.

The following code I show you, contains the integer static variable "cake" and a static 'lock' object for the Synchronized block.

I think that obviously the question is: If using "static" is supposed to be (for many people) a bad practice, is there any alternative way to solve these kind of problems without using "static"?


 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Robol Trix wrote:. . . My teacher.

Who is correct.

What do you think about the "static" declaration? . . .

Easy. If you don't have a good reason to make something static, I think it is a serious mistake to make it static.
Having a single lock object in your example means that only one thread can access the code at any one time. You can have multiple objects, but they are all locked on the same thing. Only one object can be accessed by the thread accessing the lock, so will you get any concurrency at all?

Please don't double‑space code; it doesn't aid legibility.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic