473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

Java threads and more on memory model

My first article in this series was as follows
http://bytes.com/topic/java/insights...ls-article-1-a

Here I will discuss a few more things

1> Which operations are atomic in Java?

Ans: Writes to volatiles, writes to references, and writes to 32 bit or smaller scalar values. While one of these operations have started by a thread to a particular variable, the thread must finish it before any other thread tries to write to the same variable. So these assignment operations may be used anywhere in the code without producing any garbage value due to intermediate thread interfarence.

2> Is a write immediately visible to another thread?

Ans: If a write operation by one thread T1 is followed by a read operation by another thread T2 it is not obvious that T2 will see the value written by T1. because T1 may read the value from it's local cache, which is an old value.

3> How to make it visible?

Ans: If the variable is volatile then T2 will always see the value written by T1.

4> Which operation may produce garbage value when accessed by more than one thread?

Ans. Writes to non-volatile 64-bit values (i.e., longs and doubles).

5> Why is synchronization required?

Ans. Say there are 4 code blocks (snipet) which we never want to be executed by more than one thread. This means that when any one of these blocks is being executed by a thread T1, then the same block or any of the other 3 blocks will not execute by any other thread T2. T2 must wait until until T1 finishes. as T1 finishes then only T2 will enter into any of these 4 blocks and execute it.

6> How is this acomplished?

Ans. This is acomplished by the word synchronized keyword.
An object of any class A need to be created as below.
Expand|Select|Wrap|Line Numbers
  1. A obj = new A();
Then all those code blocks need to be enclosed as below

Expand|Select|Wrap|Line Numbers
  1. synchronized(obj){
  2.  
  3.   //Code block: i.e. Lines of code follows
  4.  
  5.  
  6. }
Sep 18 '13 #1
0 4247

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rob | last post by:
Hi there, Firstly, apologies for x-post but I'm hoping the post is relevant to both groups... I have written a native implementation in C, and a java wrapper class. This all works fine and...
21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
4
by: Alan Gifford | last post by:
I wrote a program to make sure that new would throw a bad_alloc exception if more memory was requested than was available. On my system, new allocates up to 2931 MBs of memory (I don't have that...
1
by: PKJ | last post by:
Hi, Can anyone please send me some good online stuff on memory model in C. Thanks in advance.. PKJ
6
by: Lee Leahu | last post by:
Hi List, I am trying to write a simple threaded application which will simulate 1000 connections to a remote service in order to "stress test" that the remote service can handle that many...
0
by: Chris Thomasson | last post by:
<wkaras@yahoo.comwrote in message news:1156863246.290976.191710@i3g2000cwc.googlegroups.com... volatile has nothing to do with the memory model... Well, except in Microsoft... ...
10
by: 123456prakash | last post by:
Hi all , I am new to this . This is my first post . I really need some help from you all members of this forum. My situation is something like this . I am communicating with 2 system .Both the...
0
by: dhruba.bandopadhyay | last post by:
I am using the free Borland C++ 5.5 compiler. When I compile my program I notice __TINY__ constant has been defined because it runs through __TINY__ codes in the header files. How can I change this...
17
by: kumar | last post by:
hi i want to know where & how the C variables gets stored i mean like volatile , pointer and string variables gets stored , whether it is on stack or some other places if is there any clear...
13
by: eternalLearner | last post by:
i am writing an client application that connects to the server and then sends data to server. but, the problem is that i get a java.net.ConnectException: Connection refused error. The code i have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.