How to implement multi threading without using Java's synchronization

I have a shared Object called "tradeObj" whose attributes are "Price" and "Quantity".
Thread1 is going to change the price and Thread2 is going to change the quantity but not stepping on each other's update.
Because of the cost of synchronization, I dont want to use "Synchronize", but still want to make sure both threads make their update in a proper manner.
Any ideas?

Soph wrote:
I have a shared Object called "tradeObj" whose attributes are "Price" and "Quantity".
Thread1 is going to change the price and Thread2 is going to change the quantity but not stepping on each other's update.given above I would guess [your interviewer|http://forums.sun.com/thread.jspa?messageID=10976469#10976469|interview] had in mind reducing locks contention (google for "java concurrency locks granularity" or "java concurrency locks contention" if you're interested)
// disclaimer: I didn't test this code with compiler
class TradeObj {
  private int price, quantity;
  // locks
  private final Object priceLock = new Object();
  private final Object quantityLock = new Object();
  public int getPrice() {
    synchronized (priceLock) { return price; }
  public void setPrice(int price) {
    synchronized (priceLock) { this.price = price; }
  public int getQuantity() {
    synchronized (quantityLock) { return quantity; }
  public void setQuantity(int quantity) {
    synchronized (quantityLock) { this.quantity = quantity; }
Because of the cost of synchronization, I dont want to use "Synchronize", but still want to make sure both threads make their update in a proper manner.what you want above doesn't make much sense to me sorry
Any ideas?Edited by: gnat on Apr 23, 2010 9:44 AM

Similar Messages

  • How should implement multi-thread in single-threaded Operating system using

    How should implement multi-thread in single-threaded Operating system using java?
    Java supports "Multi-threading".Is there is any way run the multiple threads (Implementing multi threading) using java in a Single-threaded Operating system (That is the operating system does not support for multi-threading).

    Previous questions from OP suggest they are using J2ME, so the question might be possible.
    806437 wrote:
    How should implement multi-thread in single-threaded Operating system using java?
    What is the actual question/problem?
    A java app doesn't do threads or not do threads. It uses classes. The VM does threads.
    So if you have a platform that does not have threads and you want to support the thread class then the VM, not a java app, must provide some pseudo mechanism, such as green threads, to support that.
    If your question is about java code and not the VM then you must build a task engine and insure that the tasks are of short enough duration that it is an effective use for your system.

  • How to upload the file without using java components?

    Hello,
    I need to upload the file using JSP, but I don't want to use java componets(such as
    jspsmart).
    Who can tell me how to realize it? This issue is very important to me,
    thanks all in advance!

    make you're own servlet using the multipartrequest from o'reilly.
    easy and simple, no jspsmart, only 1 jar file and some descent programming.
    I use it like that...

  • How to implement single sign-on using java?

    I need your help regarding the following task, please go through it and tell me if you have a solution to it.
    DSOWeb is a portal which has links to all the reports generated from Microstrategy8.0.1 (MSTR) [it is another tool which generates the BI Reports] and my requirement is like when a report link in DSOWeb is clicked it goes to MSTR and shows a report of MSTR but the user is unaware of all this that the system is entering into some other portal and giving that report to him.
    1. User logs into DSOWeb (Implemented using Struts framework) - He is automatically logged into MSTR (Java Spring Architecture) as well.
    How to get the session Id of MSTR from DSOWeb and maintain that session within the DSOWeb???
    2.User clicks on a report link - He either uses the session created above or a new session is created for him, if the old one no longer exists.
    3.When User clicks Logout in DSOWeb the system should also internally invalidate the MSTR Session and logout from MSTR .
    Note : Here DSOWeb and MSTR applications are running in different Servers.

    Hello Meghal,
    It is possible to implement social login via Facebook for SAP Enterprise Portal 7.3 by simply using the SAP Cloud Identity offering.
    More details about SAP Cloud Identity you will be able to find here:
    SAP Cloud Identity Solution Brief:  Simplify and Secure Cloud Access to Critical Business Data
    SAP Cloud Identity features - latest release: http://scn.sap.com/community/security/blog/2014/12/18/new-capabilities-with-the-latest-release-of-the-sap-cloud-identity
    Please, find also the documentation about social login implementation:
    Enable or Disable Social Sign-On for an Application
    Best regards,
    Donka Dimitrova

  • How to implement multi threading in pl/sql

    Hi,
    I have to run a pl/sql procedure which calls multiple instances of another procedure of some other package.
    How should I proceed to do it?
    Bhaskar.

    Actually the final table that need to be updated has
    millions of records.
    Hence I am thinking of multithreading.1) Why are you updating millions of records? Frequently, it is more efficient to do a direct path insert of the final data from a staging table into the final table rather than trying to update millions of rows in place.
    2) Are you using parallel DML? SQL already provides the ability to enable parallelism if that is going to improve the performance of your queries. There is, in general, no need to re-implement that in PL/SQL.
    And all the information are communicated within the
    same session.I'm not sure I understand what you're saying here...
    Justin

  • How we close Views without using Java Script

    Hi All,
    can you please suggest me how i close view with out using java script.
    if possible please reply with code.
    Thanks
    Siva

    Hi Siva,
    You can do this either by hiding your window or by closing the window. Following is the sample code to do that
    IWDWindowInfo windowInfo = null;
    IWDWindow window =null;
    windowInfo = (IWDWindowInfo)  wdComponentAPI.getComponentInfo).findInWindows("<<WindowName>>");
    if(windowInfo!=null)
    window = wdComponentAPI.getWindowManager().createWindow(windowInfo,true);
    window.hide();
    Try with this code, might be useful for your scenario.
    Thanks,
    Sandeep

  • How to "kill" AWT Event Queue thread without using System.exit()?

    When I run my program and the first GUI window is displayed a new thread is created - "AWT-Event Queue". When my program finishes, this thread stays alive and I think it causes some problems I have experienced lately.
    Is there any possibility to "kill" this thread without using System.exit() (I can't use it for some specific reasons)

    All threads are kept alive by the JVM session. When you use System.exit(int) you kill the current session, thus killing all threads. I'm not sure, though...
    What you could do, to make sure all threads die, is to make ever thread you start member of a thread group. When you want to exit you app, you kill all the threads in the thread group before exit.
    A small example:
    //Should be declared somewhere
    static ThreadGroup threadGroup = new ThreadGroup("My ThreadGroup");
    class MyClass extends Thread {
    super(threadGroup, "MyThread");
    Thread thread = new Thread(threadGroup, "MySecondThread");
    void exit() {
    //Deprecated, seek alternative
    threadGroup.stop();
    System.exit(0);
    }

  • How to write a messenger with using java?

    May I know how to write a messenger with using java? The messenger need included two functions which are file transfer and add users to join the conversation.
    Thank you.

    Ok, so you need to start from the beginning, the requirements, is it a swing (i.e. GUI) app, web app, console, etc...?
    As stated by sprizor making an IM client is no easy task, you need to both implement the server side and the client side, which both communicate with each other, file transfers are also quite complex, and even maintaining a multi-user chat can be quite difficult.
    If you are after a Web Based app, then you will need to look into Push technologies, like Grizzly Comet which is a nice wrapper for the NIO java stuff:
    https://grizzly.dev.java.net/
    Good luck...

  • How to send HTML Format Mail using Java Mail in oracle 9i Forms

    Dear All
    could you please tell me how to send HTML Format Mail using Java Mail in oracle 9i Forms and how to implement the java mail ?
    if it is possible, could you please send me the sample code? please very urgent
    Thanks
    P.Sivaraman

    Hello,
    <p>Here is a Form sample.</p>
    Francois

  • Multi-Threaded server using  ThreadPool

    Dear friends,
    I am writing a client-server program in which the client and server communicate using SUN-RPC. The client reads a file containing some numbers and then spawns threads which it uses to request the server for a service. These threads are executed using a thread pool. Till this time, it's working fine. But when it comes to the server, the real trouble begins because it too needs to be made a multi-threaded one using thread pooling. The server has to capture the call information for each request for the service and then pass this information to a thread/runnable object in whose run() method the code for execution of the service would be present. Since the tasks(requests for the service) are not present already, i am unable to execute the server side threads in a loop using a thread pool. How to solve this problem? Kindly help.
    Thanks,
    Subhash

    The server has to capture the call information for each request for the serviceWhy?
    and then pass this information to a thread/runnable object in whose run() method the code for execution of the service would be present.Why can't the run() method get the call information when it starts? That's what's normally done. The server's accept loop mustn't do any other I/O: otherwise a rogue client can block the server complete.y

  • Implementing n-tier Architecture using java

    Hi !!
    I wish to know how I can proceed to implement n-tier application using java
    or how to implement n-tier application with JBuilder.
    Thanks!

    Easypublic class nTier {
      public static void main(String[] args) {
        int n =5;
        for (int i=0; i<n; i++) {
          System.out.println(i + " tier");
    }Ted.

  • Can anybody know how to configure Multi threaded server?

    Hi
    All,
    Can anybody know how to configure Multi threaded server?
    Thanks,
    Vishal

    Values are just samples only. use what ever appropriate for your environment. Understand each of them before using in production.
    alter system set DISPATCHERS="(PROTOCOL=tcp)(DISPATCHERS=3)(CONNECTIONS=1000)"
    alter system set shared_servers=100
    replace "DEDICATED" with "SHARED" in tns names
    Ready to go.
    select username,server from gv$session (server should show none or shared)

  • How we generate Surrogate Keys without using identify column

    Hi All,
    How we generate Surrogate Keys without using identify column.
    Regards,
    Manish

    There are various options
    1.IDENTITY columns - simplest to implement
    2. Using NEWID(), NEWSEQUENTIALID() functions (if you want to use GUID values as surrogate keys)
    3. SEQUENCE object (if SQL 2012 and above)
    4. Using custom functions to generate keys yourself
    This is an good article which compares use of GUIDs against integers as surrogate keys
    http://blog.jonathanoliver.com/integers-vs-guids-and-natural-vs-surrogate-keys/
    Please Mark This As Answer if it solved your issue
    Please Vote This As Helpful if it helps to solve your issue
    Visakh
    My Wiki User Page
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • How to retrieve data from MDM using java API

    hi experts
    Please explain me the step by step procedure
    how to retrieve data from MDM using java API
    and please tell me what are the
    important classes and packages in MDM Java API
    thanks
    ramu

    Hi Ramchandra,
    You can refer to following links
    MDM Java API-pdf
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/2d18d355-0601-0010-fdbb-d8b143420f49
    webinr of java API
    https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/89243c32-0601-0010-559d-80d5b0884d67
    Following Fourm Threads will also help.
    Java API
    Java API
    Re: usage of  java API ,
    Matching Record
    Need Java API for Matching Record
    Thanks and Regards,
    Shruti.
    Edited by: Shruti Shah on Jul 16, 2008 12:35 PM

  • How can i get system variable using java

    Hi,
    I just want to know how can i get system variables using java code.
    for example i want to get the the date for today or i want to get the number of processes that's running.
    Thanks alot

    Hi,
    I just want to know how can i get system variables
    using java code.
    for example i want to get the the date for today or i
    want to get the number of processes that's running.
    Thanks alotSome generic "system variables" are available though Java, usually through the System class.
    Date today = new Date();
    is instantiated with the current date and time.
    Other system values, like environment values, should be passed to java through the command line (-D option) by setting system properties.
    Finally, platform specific values like the number of processes running will have to be written in platform specific code and executed by JNI (java native interface).
    Java is platform or system agnostic. Common system values, like time, are implemented. Hopefully you won't need platform specific values.

Maybe you are looking for

  • I cannot open or print pdf. files with Adobe on my windows 8

    I have downloaded Adobe Acrobat but can only read files not print them - even when I save them.

  • More than 1.25GB RAM

    I have a 12" G5 1.5GHz PowerBook with 512MB RAM. 1) Can I upgrade the RAM to more than 1.25GB? I'm sure this was the limit imposed on it by Apply when I bought it - or was this just due to 1GB being the maximum available at the time? 2) Will I notice

  • What is Retro active rebates and how there are configured

    Hi, What it means by retro active rebates and how there are configured. Please also explain how rebates settlement will happen... Regards Sankar

  • Sync TX 5.4.9 with Mac

    TX 5.4.9 wont sync with Mac Snow Leopard. I installed Mark Space software 'Missing Sync for Palm OS' but it assumes the Mac should override the Palm -I work the other way round. Should I uninstall Mark Space and Snow Leopard and install Leopard and m

  • J2EE Architectural Design Solution Needed

    Hi, I am coming up with an issue in designing the application architecture. It would be great if some one suggest solution for the problem. The basic flow of application: 1. Client initiates a transaction from a Portlet. 2. Portlet invokes business s