Results 1 to 6 of 6

Thread: Is Global synchronization possible

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Is Global synchronization possible

    Hello,
    My problem is- I want to make a distributed application that is responsible for breaking down a "Job" in a number of smaller "jobs" (eg calculating Pi to a certain number of decimal). Every little job is executed in a separate thread. (In our example, the first thread calculates the first 10 decimal places, the following 10 second, and so on. Ultimately, the main thread is responsible therefore to launch as many threads it is, initializes correctly (In our example, it indicates the limits of computation, ie [0-10] for 1 thread, [11-20] for 2nd ...), and waits for each thread to finish. I would notify the user at the end of each small job, for he sees that stage is reached. For this I thought to loop with a wait () but I think it is not exactly what I want. Indeed, if we loop from 0 to nb_thread by a wait () on the thread i [um, am I clear enough?], It is possible that the thread i +1 has already finished and the thread i still wait. When the thread i will finally calculated its part, we will then notify the end of stage i and also that of stage i +1.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Is Global synchronization possible

    Hi,
    Well, I'm not sure I understand everything, then I begin by summarizing the problem. Looking in fact have a main thread (a sort of head) which will fly a certain number of threads, and that he will delegate the work. And when a thread and "executive" has finished his work, it informs his "boss", knowing that there is no assumption about the order. That's how I see it in Java. Your Thread "head" and creates Threads launches its "executive" and then he fell asleep with wait (). And whenever a running thread completes its work, it wakes up the thread "head" with notify () (sorry for the metaphor, the leaders will not be happy! lol !!!!). And whenever the Chief is awakened it looks at the status of each executive (because many of them may end at the same time).

  3. #3
    Join Date
    Dec 2009
    Posts
    263

    Re: Is Global synchronization possible

    Hi,
    So I actually misread the documentation. It must be
    Code:
    this.wait();
    not
    Code:
    Threads[i].wait();
    Indeed, it sounds pretty clear like that. I was not having the eyes in front of the hole. And thus it will be the first thread that will end (will the notify (), then) who wakes up the main thread (or head ...) It will not be following a list. I think the equivalent code gives roughly this.
    Code:
    for(i =0; i <nb_threads; i + +)
        Threads[i].home();
     
    for(i = nb_threads; i>0;)
    {
        this.wait();
        i = this.activeCount();
        System.out.System.out.println("It remains"i + +Thread (s) in progress ");
    }
    Any ideas about this code.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Is Global synchronization possible

    Hello,
    It's not quite it. The wait () / notify () must be done on the main thread. So the Threads "executive" must have a reference Thread father, something like this:
    Code:
    Public class Executive extends Thread {
      private Thread thpri;
      private boolean iswork;
    
      Public Executive(Thread thpri) {
        this.thpri = ThreadPrincipal;
      }
     
      Public synchronized void setWorking(boolean iswork) {
        this.iswork = IsWorking;
      }
     
      Public synchronized boolean iswork() {
        return iswork;
      }
     
      Public void run() {
        setWorking(true);
        / / Do the processing
        end();
      }
     
      Public synchronized void end() {
        setWorking(false);
        thpri.notify();
      }
     }

  5. #5
    Join Date
    Dec 2009
    Posts
    263

    Re: Is Global synchronization possible

    Hello,
    Ok, I think I have now understood globally. Thank you very much. A few small details. Each instance of Executive will have its own value isWorking. But if we had put synchronized is there was only one variable isWorking distributed between threads? Is the scope of the variable (uh I know if we use the term scope to refer 'private', 'public', 'protected'...) Will change anything? Otherwise, for the main thread, activeCount () does not work? Since the function is called after the wait ()The notify () has been executed and the thread Executive released its run (). So it should not be recognized by activeCount ().

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Is Global synchronization possible

    Hello,
    But if it had been synchronized is there was only one variable
    isWorking distributed between threads?
    I do not think so. This is the keyword static to denote that a variable is shared between instances of the same class. The synchronized used to manage concurrency.
    Otherwise, for the main thread, activeCount () not working?
    If it can work, provided you do not have other threads in the program. Otherwise, I think that there are Thread Group precisely to consolidate the threads between them. I am not sure how it works, but it may be interesting to look in that direction.

Similar Threads

  1. WI-FI synchronization in IOS 5
    By Ashokvardan in forum Portable Devices
    Replies: 10
    Last Post: 27-10-2011, 03:20 PM
  2. Synchronization System for Maxthon 3
    By Hadriel in forum Technology & Internet
    Replies: 6
    Last Post: 01-07-2011, 10:50 PM
  3. Replies: 3
    Last Post: 07-08-2010, 12:33 PM
  4. LAN games with Synchronization
    By Jasonholt in forum Video Games
    Replies: 7
    Last Post: 07-04-2009, 11:12 AM
  5. Synchronization of databases
    By Dadhij in forum Software Development
    Replies: 4
    Last Post: 15-12-2008, 07:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,092,786.96350 seconds with 17 queries