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

Synchronization

ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hi,
i am new to java programming and i need help. actually i am building the Global positioning system application. so whenever it retrieves the location values it uses gprs class to send it to some URL OK. so that gprs class can be used by other application at same time to send other data by some other application. what i mean to say is that gprs class is used by many applications like Global positioning system and so on. there should not be conflict b/w applications when using General packet radio service code so whats the solution for it. how exactly i need to do synch between diff classes when using General packet radio service class.
i don't have much idea about synch so please help out. thank you

Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I'd start by reading up on how concurrency works in Java, and what mechnisms it has for dealing with the problems that arise: http://download.oracle.com/javase/tutorial/essential/concurrency/index.html
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
As far as i have understood you said that a GPRS class is used by a number of different applications. I suppose that this GPRS class has been developed by you.
One design solution can be that you shall make the data processing and sending methods of GPRS class static and synchronised on the Class.
I have made an example program to mimic some what you want.
Here,
GPRSClass is used by different applications, different applications have been represented by instances of Application Class.
Hope this is useful for understanding.






When this code is run it gives output something like this(will vary but different applications can not interfere, hence mutually exclusive) -

Application: 0
Shared Data Value: 0
Application: 0
Shared Data Value: 1
Application: 0
Shared Data Value: 2
Application: 0
Shared Data Value: 3
Application: 0
Shared Data Value: 4
Application: 2
Shared Data Value: 5
Application: 2
Shared Data Value: 6
Application: 2
Shared Data Value: 7
Application: 2
Shared Data Value: 8
Application: 2
Shared Data Value: 9
Application: 1
Shared Data Value: 10
Application: 1
Shared Data Value: 11
Application: 1
Shared Data Value: 12
Application: 1
Shared Data Value: 13
Application: 1
Shared Data Value: 14

ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Nomaan Butt,

Actually i have written the code for it please can you have a look on it and tell me what the problems or errors i may face. i will show only the imp part of the project.there are four classes GPS, Main , locatioonlistenerexample and GPRS. At present my project is like i need to retrieve the GPS location and send that values through GPRS to some URL. Now only we using gprs to send the GPS location but later many other applications come into picture which uses the gprs to send data. at present i am working on GPS using GPRS so in future this following code should not lead to an error so suggest if there any errors that may occur in future when many applications uses GPRS

class Main{
public static void main(String[] args){
Gps ref = new Gps();
ref.locationprovider(); }
}

class Gps{

locationprovider(){

here it assigns the location provider and then calls the location listener method.

}

listener()
{
here it assigns the listener and then schedules the listener for few secs lets take 60 secs }

}


public class LocationListenerExample implements LocationListener{


public double latitude, longitude;
Gprs runnable = new Gprs();


/after every 60secs this locationupdate is automatically called and updated the location value. its as per the GPS API
public void location Updated(LocationProvider locationprovider,
Location location){
.......
......
......
if (latitude!=0 && longitude!=0) {
synchronized(runnable) {

runnable.latitude = latitude;
runnable.longitude = longitude;
Thread ref = new Thread(runnable);
ref.start();


}}}

}


class Gprs{

double lat, long;
public synchronized void run(){

here it sends that lat and long value to URL

}

}



Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
hi ashu,

GPRS class need not to be a runnable object, you can't make a new thread of GPRS class whenever it is required by different applications.
Instead make it a normal class and synchronize the methods of this class like GPRSClass which i showed in my previous post. In GPRSClass
GPRSDataProcessor() method is static and synchronised on the class object. Therefore any application which you may create in future, which has the lock on class object of GPRSClass can access it exclusively.


ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

thanks a lot. can you please alter the above code and write in the logic which you said. because i am not getting the clear idea of it. it will be helpful.

:-)
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54


this is not required, instead follow singleton class approach

ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

Really thanks a lot lot. i learn't a new trick from you. but i have one doubt. Every time we are going to call from application the GPRS.getInstance().processing(); so my doubt is that every time it creates the new instance of GPRS class which may lead to many objects created on the heap right and heap memory will be overflowed.

i think i am wrong if you don't mind can you explain it. please

ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

I want to learn more and more tricks in java. so can you suggest me any of the books to go through.


Thanks:-)
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
Every time we are going to call from application the GPRS.getInstance().processing(); so my doubt is that every time it creates the new instance of GPRS class which may lead to many objects created on the heap right and heap memory will be overflowed.



above code checks weather object of GPRS class exists in memory or not, it will create only if object doesn't exist already.
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
I want to learn more and more tricks in java. so can you suggest me any of the books to go through.

I don't know any book for tricks,
"practice makes man perfect", you can refer kathy sierra java book for SCJP for good understanding of basic concepts
ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

Thanks a lot for the help. ya i wanted to know the basic concepts. ok i wil go through that book.

once again Thanks a lot lot.:-)
ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

last and final doubt see now this gps application gets the location value for every minute and calls the gprs to send this values. according to you it calls this GPRS.getInstance().processing();
So every time when it calls a method then stack stores some of the values like return type and all of that method. so if we call this method for every minute does it lead to stack over flow exception.
:-)
:-)
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
no need to be the last,,

stackoverflow shall not happen, it happens in incorrect coding, whenever gps will need to send data through gprs it will need to call some method or the other of gprs class. you can't avoid this.
ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

Thanks a lot:-) for explaining all the questions(silly) by me....:-)
ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

Again i am facing the other problem,

private GPRS{
//any initalization
}


when i add this line then it shows the following error:

Syntax error on token "private", class expected after this token
- Syntax error on token "Gprs", delete this token


and even how to access the variable latitiude and longitude values which we get in classLocationListenerExample. so i need this values send through the gprs class. so how can i retrive them in synchronized manner.:-(
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54

in the above lines if you want to define constructor for GPRS class then


you can use semaphore where you are writing to lattitude or longitude, if they are being shared by other class(or you can say applications in your case). Reading shared data does not need locks.

you can do like this-



in the above code scope of semaphore should be where shared data is being written.


In your code which you posted i have not seen any write to longitude or latitude.




ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

if (latitude!=0 && longitude!=0) {
synchronized(runnable) {

runnable.latitude = latitude;
runnable.longitude = longitude;
Thread ref = new Thread(runnable);
ref.start();
}
}

this way i used to read the long and lat values where runnable is of gprs object.
those lat and long values are not shared by any applications. just my project(gps tracking) is like need to retrieve the value and send through gprs that's it. so how to use the values in gprs class which we got into the location listener class. i am using the same logic which you gave so how to use them in gprs class so that those values can be sent to some url.
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
pass them as arguments to processing method of gprs class

ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
i got this idea noman like passing parameters to process method. but problem is that same method is used by other applications right then they invoke this method process. then how about the parameters:-(

i need to write in general way so that applications can use this gprs class according to there requirements.
ex gps class use to send the lat and long values and some other class use to send some other values etc.....


so whats the solution for this?
should i write in general way like process method by all applications or any other way?

hope you got it what i am trying to explain:-)
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
If i have understood you mean that processing() method will act on different amount/type of data from different classes.
To achive this you can do


if the type of data is different then some other solution will have to worked out. I suggest you should keep the type string, and use Integer.parseInt(), Double.parseDouble() etc methods on string data whenever required.
ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
hello Noman,

for each loop is not supported by java 1.4 version because its introduced in java 15. version. The hardware on which i need to run this program(application) supports only java 1.4 version. so is there any alternative way to do the above task instead of for each loop. or any other solution would you like to suggest?

please help me:-)

Thank you
Nomaan Butt
Ranch Hand

Joined: Oct 19, 2011
Posts: 54
use the normal for loop then
ashwini kalmath
Greenhorn

Joined: Oct 24, 2011
Posts: 25
Hello Noman,

oh ok ok. Thanks a lot
 
Don't get me started about those stupid light bulbs.
 
subject: Synchronization