• 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

What does "thread safe" mean?

 
Ranch Foreman
Posts: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
My murach Java book keeps using the term "Thread Safe"  
What does this mean?
Thanks,
Kevin
 
Bartender
Posts: 2911
150
  • Number of slices to send:
    Optional 'thank-you' note:

kevin Abel wrote:My murach Java book keeps using the term "Thread Safe"  
What does this mean?...



Has the book defined that phrase ? if yes, What is your understanding about the book's explaination ?
 
kevin Abel
Ranch Foreman
Posts: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
Salvin,
I looked in the table of contents and it is not listed.  
The next time I run into it in the book I will come back to CodeRanch.
I appreciate the response.
Thanks,
Kevin
 
kevin Abel
Ranch Foreman
Posts: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
I found some others asking about "thread safe" in Code Ranch.  It has something to do with multiple threads accessing the same classes.  Also something about AWT being newer than the old GUI thing. I'm not using multi-threading so I think I can learn about this later on.
 
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:

kevin Abel wrote:. . . AWT being newer than the old GUI thing. . . .

You have read that part wrongly. Regard AWT display components as obsolete.
 
Saloon Keeper
Posts: 27871
196
  • Number of slices to send:
    Optional 'thank-you' note:

kevin Abel wrote:I found some others asking about "thread safe" in Code Ranch.  It has something to do with multiple threads accessing the same classes.



Nope. It has everything to do with how multiple threads access the same resources. The shared resource may be a class or a component of a class. If the access to that resource is controlled (via Java synchronization), then it's (probably) thread-safe.

AWT does have some utility still, I think, but it's dead last when considered for use as a desktop GUI. It's slower and less efficient than Swing, uglier, and less able to integrate well into the machine's underlying windowing/GUI system.

The reason why I hesitate to pronounce AWT as dead is I'm fairly sure that there are parts of Swing that do build on AWT.

And, incidentally, there are alternatives to both, despite the fact that AWT and Swing are part of the core JRE (at least before Oracle started partitioning it). The Eclipse IDE was built on a third-party GUI framework called SWT and it's not the only major GUI product to be developed so.
 
Campbell Ritchie
Marshal
Posts: 79424
377
  • Number of slices to send:
    Optional 'thank-you' note:

Tim Holloway wrote:. . . AWT does have some utility still . . . there are parts of Swing that do build on AWT. . . .

The non‑display AWT classes include Color, layout managers, and listeners, and they are all still in regular use with Swing®. Many Swing® display classes are subtypes of AWT classes, too.
 
kevin Abel
Ranch Foreman
Posts: 914
10
  • Number of slices to send:
    Optional 'thank-you' note:
Campbell,
Yes you are right.  Swing is the newer thing.  AWT is the older one.
Tim,
It reminds me of the old days of my dBase programming where two PCs running the same software were attempting to write to the same cell in a table.  They were sort of like threads but from two different machines.
Appreciation to all.
Thanks,
Kevin
 
salvin francis
Bartender
Posts: 2911
150
  • Number of slices to send:
    Optional 'thank-you' note:
When multiple threads are involved, even doing a simple task such as incrementing a number gets tricky. Basically, if there's any object that can be accessed by two or more threads and if that object allows it's data to be changed, we're in a soup  

Possible solutions to overcome this :
  • Dont share objects (not practical)
  • Ensure shared objects cannot change their data (Immutable)
  • "Put a lid over it". (My favorite solution. Simply add a disclaimer that your library or code is not "Thread safe".)
  • Add locks. (This involves using synchronized code blocks, synchronized methods, double checking etc...)
  • Point 4. above needs a lot of thought process and may not always be the right solution and can cause other problems like deadlocks, starvation and the worst of all: Performance.
    Sometimes there's no right solution !!
     
    Sheriff
    Posts: 7125
    184
    • Number of slices to send:
      Optional 'thank-you' note:
    For just incrementing and decrementing you could use AtomicInteger or AtomicLong.
     
    Sheriff
    Posts: 17652
    300
    • Number of slices to send:
      Optional 'thank-you' note:

    kevin Abel wrote:
    It reminds me of the old days of my dBase programming where two PCs running the same software were attempting to write to the same cell in a table.  They were sort of like threads but from two different machines.


    You're aging yourself and so will I now. One of the first environments I did non-trivial programming in was dBase—II, III, IV, and xBase (FoxBase, FoxPro, and my favorite at the time, Clipper)—and yes, thread-safety issues are very similar to what you mentioned. Immutable classes are inherently thread-safe because once created their state won't change. Anything that is mutable should be examined and tested carefully for thread safety if there's a chance it will be used in a multithreaded environment. Essentially, it boils down to maintaining and using shared state in a way that preserves the integrity of data and logic.
     
    Greenhorn
    Posts: 23
    • Number of slices to send:
      Optional 'thank-you' note:
    About the term "thread-safe" ::   I think it means that if a running piece of code is executing normally,  and it needs to create a new separate thread to perform a separate task,   ( like updating some remote data access,  then the new threads is not allowed to disrupt any other thread that may be accessing the same resource.  The disruption could cause data to be lost or distorted if two different treads tried to do an update operation at the same time.  "Thread-safe"  code protects against that....  
     
    Junilu Lacar
    Sheriff
    Posts: 17652
    300
    • Number of slices to send:
      Optional 'thank-you' note:

    Andrew Jerpe wrote:I think it means that if a running piece of code is executing normally,  and it needs to create a new separate thread to perform a separate task,   ( like updating some remote data access,  then the new threads is not allowed to disrupt any other thread that may be accessing the same resource.  The disruption could cause data to be lost or distorted if two different treads tried to do an update operation at the same time.  "Thread-safe"  code protects against that....


    This seems like a valid scenario but it's also a very specific example. Thread-safety in general covers way more scenarios than this.
     
    Tim Holloway
    Saloon Keeper
    Posts: 27871
    196
    • Number of slices to send:
      Optional 'thank-you' note:
    It's also worth noting that thread are intra-JVM constructs and are only one form of multi-tasking. You can also have multiple OS processes, you can have multiple CPU cores (tightly-coupled multi-processing), multiple-CPU motherboards and multiple physical machines (loosely-coupled multi-processing).

    Take DBase. With a few exceptions DOS was not a multi-tasking OS, but if a DBase or FoxPro database was on a shared fileserver (say, a Novell LAN), then there was a potential for corruption and/or denial of service. That's because the only level of resource locking available was at the file level - when it was employed at all and there was no central monitor to ensure that the different users were informed when what was in their local buffers was stale info because someone else had altered the file. That's not threading at all. It's multi-processing at the loosest level.

    Java's "synchronized" features apply only to threads within the current JVM. Anything requiring co-ordination at any larger scope - including between 2 different JVMs - requires other mechanisms.
     
    kevin Abel
    Ranch Foreman
    Posts: 914
    10
    • Number of slices to send:
      Optional 'thank-you' note:

    Tim Holloway wrote:That's because the only level of resource locking available was at the file level - when it was employed at all and there was no central monitor to ensure that the different users were informed when what was in their local buffers was stale info because someone else had altered the file. That's not threading at all. It's multi-processing at the loosest level.

    Java's "synchronized" features apply only to threads within the current JVM. Anything requiring co-ordination at any larger scope - including between 2 different JVMs - requires other mechanisms.



    Also:

    Nope. It has everything to do with how multiple threads access the same resources. The shared resource may be a class or a component of a class. If the access to that resource is controlled (via Java synchronization), then it's (probably) thread-safe.



    I'm looking back at this 3 years later.   I'm not sure about what a JVM resource is.   I'm thinking of it as a Class or method.   Is the idea that two parts of the code cannot change the values of something in them?   The code might even be running on more than one JVM.    I didn't know that a second JVM could see the objects from another JVM.  

    Thanks,

    Kevin
     
    Tim Holloway
    Saloon Keeper
    Posts: 27871
    196
    • Number of slices to send:
      Optional 'thank-you' note:
    When I say "resource", I mean stuff like a specific variable instance, an I/O provider (file, network connection, or whatever) or even other threads. Basically anything that you can do something with.

    JVMs cannot access each other's memory, threads, or other internal resource, but they can share external resources, like when 2 different Java apps want to write to the same file at the same time. However "synchronized" access doesn't cover that. Stuff that's declared synchronized is only guarded against concurrent access by other threads in the same JVM. To synchronize with other OS proccesses (JVMs or otherwise) requires OS synchronization services, which may not be avaiable to pure Java code.
     
    Greenhorn
    Posts: 3
    • Number of slices to send:
      Optional 'thank-you' note:
    I make a simple semaphore. A simple bool or int. This is a simple gate.
    A thread can only do something when the gate is open. Say sem=1.
    If sem==0 the thread simply waits.
    So if sem==0 then FIRST you close the gate. Make sem 1.
    THEN you do the work. If the work is ready, then as last action you open the gate.
    Works perfect.
    Important thread you use pauses of say 20 msec
    A not important thread, for example show internet loaded value in a gui field, you use pauses
    Of say 500 msec.
    Works perfect.
     
    hans brink
    Greenhorn
    Posts: 3
    • Number of slices to send:
      Optional 'thank-you' note:
    Sorry for writing error in my semaphore text. I swapped the values 0 and 1 .
    So for example you make a common basket , protected by a common semaphore.
    The important high speed,  http thread fills the http received value in the basket.
    The less important, slower , thread takes this value out the basket and shows it in the
    Gui field.
    Between closing the gate and the code for the action it is possible, that the cpu divide the code....!!! But Java has a simple solution: set closing the gate and the action simply in one not dividable code block.
    Baron
    reply
      Bookmark Topic Watch Topic
    • New Topic
    vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
    http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |