Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners java implementation problem

Have Programming Question? Ask it here!
 
 
 


Mohit Kumar Jangid
java implementation problem
2 Answer(s)      a year and 8 months ago
Posted in : Java Beginners


I want to implement following in java code :

Main thread

  1. Create three threads
  2. wait for completion of stage 2 of all three threads

  3. Access all three local variable (LC0, LC1, LC2) of threads

  4. bulid a new main data (MD)

  5. Resume all three threads execution

  6. Wait for end of each thread

  7. Acess all three local variable (LC'0, LC'1, LC'2)

          Each threads executes following
    
    
    
         1. Build a local variable (LC) 
         2. wait till main thread signals in stage 5 (of main)
    
    
         3. access a main data (MD)
         4. update local data (LC'= updated LC)
         5. do something with local data (LC')
         6. end
    

My problems:

  1. There is no need of synchronization. hence i can't use wait and notify.

  2. How to access local data of threads in the when threads is paused ( is waiting).

  3. How to resume execution of all three threads.

I will expain the algorithm if required.

Please help me.


Advertisement
View Answers

November 5, 2012 at 11:15 PM


so my whole day has to be wasted for answer by ME ONLY.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread.work;

import com.sun.corba.se.spi.monitoring.MonitoredAttribute;
import com.sun.corba.se.spi.monitoring.MonitoredObject;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MKJ
 */

class mods{  

  static Object mo1=new Object();
  static Object mo2=new Object();

  boolean wasSignalled = false;

      public void doWait(Object mo){
        synchronized(mo){
          if(!wasSignalled){
                try {
                    mo.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(mods.class.getName()).log(Level.SEVERE, null, ex);
                }            

                //clear signal and continue running.
                wasSignalled = false;
         }      
        }
      }

      public void doNotify(Object mo){
        synchronized(mo){
          wasSignalled = true;
          mo.notify();
        }
      }

     void foo1(Integer x) throws InterruptedException{        


       System.out.println("before notify foo1["+x+"]");

       doNotify(mo1);             

       System.out.println("before wait foo1["+x+"]");
       doWait(mo2);
       System.out.println("after wait foo1["+x+"]");


    }

    void foo2(Integer arr[]) throws InterruptedException{

        for (int i = 0; i < arr.length; i++) {
            System.out.println("before wait foo2");            
            doWait(mo1);
            System.out.println("after wait foo2"); 

        }       

        for (Integer integer : arr) {
          System.out.print(" ["+integer+" ]");  
        }

        for (int i = 0; i < arr.length; i++) {
           doNotify(mo2);           
        }

    }
}


class ChildThread implements Runnable   {

    Thread t;
    Integer ld;
    mods theO;

    public ChildThread(Integer var,mods theO) {

        this.theO=theO;
        ld=var;
        t=new Thread(this, "child"+ld);
        t.start();
    }        

    @Override
    public void run() {
        try { 
            theO.foo1(ld);
        } catch (InterruptedException ex) {
            Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

class MasterThread implements Runnable{

    Thread t;
    Integer arr[];
    mods theO;

    public MasterThread(Integer var[],mods theO) {
        this.theO=theO;
        arr=var;

        for (Integer integer : arr) {
            System.out.println(" ["+integer+" ]"); 
        }

        t=new Thread(this,"master");
        t.start();
    }        

    @Override
    public void run() {
       try { 
            theO.foo2(arr);
        } catch (InterruptedException ex) {
            Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex);
        } 

    }

}


public class ThreadWork {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        ChildThread ft[];
        MasterThread mt;

        mods theMod=new mods();

        Integer ints[]=new Integer[3];
        ints[0]=0;
        ints[1]=1;
        ints[2]=2;

        ft = new ChildThread[3];
        mt=new MasterThread(ints,theMod);

        for (int i = 0; i < 3; i++) {
            ft[i]=new ChildThread(ints[i],theMod);            
        }        

    }
}


November 5, 2012 at 11:15 PM


so my whole day has to be wasted for answer by ME ONLY.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thread.work;

import com.sun.corba.se.spi.monitoring.MonitoredAttribute;
import com.sun.corba.se.spi.monitoring.MonitoredObject;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MKJ
 */

class mods{  

  static Object mo1=new Object();
  static Object mo2=new Object();

  boolean wasSignalled = false;

      public void doWait(Object mo){
        synchronized(mo){
          if(!wasSignalled){
                try {
                    mo.wait();
                } catch (InterruptedException ex) {
                    Logger.getLogger(mods.class.getName()).log(Level.SEVERE, null, ex);
                }            

                //clear signal and continue running.
                wasSignalled = false;
         }      
        }
      }

      public void doNotify(Object mo){
        synchronized(mo){
          wasSignalled = true;
          mo.notify();
        }
      }

     void foo1(Integer x) throws InterruptedException{        


       System.out.println("before notify foo1["+x+"]");

       doNotify(mo1);             

       System.out.println("before wait foo1["+x+"]");
       doWait(mo2);
       System.out.println("after wait foo1["+x+"]");


    }

    void foo2(Integer arr[]) throws InterruptedException{

        for (int i = 0; i < arr.length; i++) {
            System.out.println("before wait foo2");            
            doWait(mo1);
            System.out.println("after wait foo2"); 

        }       

        for (Integer integer : arr) {
          System.out.print(" ["+integer+" ]");  
        }

        for (int i = 0; i < arr.length; i++) {
           doNotify(mo2);           
        }

    }
}


class ChildThread implements Runnable   {

    Thread t;
    Integer ld;
    mods theO;

    public ChildThread(Integer var,mods theO) {

        this.theO=theO;
        ld=var;
        t=new Thread(this, "child"+ld);
        t.start();
    }        

    @Override
    public void run() {
        try { 
            theO.foo1(ld);
        } catch (InterruptedException ex) {
            Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

}

class MasterThread implements Runnable{

    Thread t;
    Integer arr[];
    mods theO;

    public MasterThread(Integer var[],mods theO) {
        this.theO=theO;
        arr=var;

        for (Integer integer : arr) {
            System.out.println(" ["+integer+" ]"); 
        }

        t=new Thread(this,"master");
        t.start();
    }        

    @Override
    public void run() {
       try { 
            theO.foo2(arr);
        } catch (InterruptedException ex) {
            Logger.getLogger(ChildThread.class.getName()).log(Level.SEVERE, null, ex);
        } 

    }

}


public class ThreadWork {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        ChildThread ft[];
        MasterThread mt;

        mods theMod=new mods();

        Integer ints[]=new Integer[3];
        ints[0]=0;
        ints[1]=1;
        ints[2]=2;

        ft = new ChildThread[3];
        mt=new MasterThread(ints,theMod);

        for (int i = 0; i < 3; i++) {
            ft[i]=new ChildThread(ints[i],theMod);            
        }        

    }
}


Related Tutorials/Questions & Answers:
Java implementation problem
/answers/viewqa/Java-Beginners/28578-java-implementation-problem-.html...Java implementation problem  I want to implement following in java... problem in your post previews. please consider 1. 2. points just after main
java implementation problem
java implementation problem   I want to implement following in java code : Main thread Create three threads wait for completion of stage 2 of all three threads Access all three local variable (LC0, LC1, LC2) of threads bulid
Advertisements
java native implementation
java native implementation  What is java native implementation
filter implementation in java
filter implementation in java   How to implement filters in java?   Java - filter implementation Tutorials Filter Files in Java Response Filter Servlet Example
Question in Array Implementation (java) ??!
Question in Array Implementation (java) ??!  Good Morning EveryOne I have Q in Java - and if any One have the Answers please tall me ??!! Question... the phone directory problem defined above. THANKS LOOT
java interfaces implementation
java interfaces implementation  hai I have defined one inter face... different implementation classes for add() method and sub()and mul() when i trying... which are define in Maths interface. I want only addition implementation
java implementation - Java Beginners
java implementation  Im M.Phil research scholar,i need a software testing module implemented in java.My research area is software testing,if anyone could help me out
garbage collection implementation dependent java
garbage collection implementation dependent java  How a garbage collection works in Java
Progress Bar implementation in Java Script
Progress Bar implementation in Java Script  Hi Friend, I am new to Java Script, but i want implement the progress bar in my html by reading the value from file (like value=30) and the value in file be changing from 0 to 100
java code implementation - Java Beginners
java code implementation  I am writing a java program, voteCounter the program is in two classes; one is the main class, voteCounter.java... counts how much each person was voted for. * the problem I'm having is I need
linked lists implementation - Java Beginners
, there are no preconditions. Write the code for the method for a linked implementation (without tail
Stack Implementation java code - Java Beginners
Stack Implementation java code  Hello roseindia and java experts can u help me.can you give me a code for this sir/maam.thank you so much. STACK IMPLEMENTATION -expression evaluator *GIVEN a String of expression, create
Simple Hash Table implementation in Java
Simple Hash Table implementation in Java   ... implementation from the basic in Java. In this section, you will see how... the given title exists or not. This section is very helpful for your java
java Problem
java Problem  I want to create a binary tree for displaying members in Downline. i am creating a site for MLM(Multi-Level MArketing). tree must be dynamically populated from database. is there any help for me. Thanks in advance
JAVA Problem
JAVA Problem  Write a program that takes two parameters 1. a word 2. an array of words It should then remove all instances of the word in the array. For Example: INPUT word="ravi" word_array = ["Chethan Bhagat
Object-Oriented Implementation of Numerical Methods - Java Tutorials
Object-Oriented Implementation of Numerical Methods 2002-04-01 The Java Specialists' Newsletter [Issue 044] - Review: Object-Oriented Implementation... of The Java(tm) Specialists' Newsletter, sent to 3101 (!) Java experts in over
resolution problem in java
resolution problem in java  I designed project in java in my PC when run the same project in some other PC i can't fully view my java forms.Some said that it is resolution problem
java input problem - Java Beginners
java input problem  I am facing a Java input problem
Java Problem - Java Beginners
Java Problem  Write a program 2 input a positive integer n and check wheter n is prime or not and also know the position of that number in the prime..., Code to solve the problem : import java.io.*; public class PrimeNumber
java programming problem - JDBC
java programming problem  Hi, Request you to provide the source code in Java for the following programming problem : upload .csv file data into oracle database. please send the solution to raichallp@yahoo.in
Problem in uploading java application
Problem in uploading java application  I have uploaded my java application (folder created under webapps) using Filezilla FtpClient.Application... this problem
Java problem - Java Beginners
Java problem  what are threads in java. what are there usage. a simple thread program in java  Hi Friend, Please visit the following link: http://www.roseindia.net/java/thread/index.shtml Thanks
for a problem in coading - Java Beginners
for a problem in coading  what is the problm in following coading...(String[] args) { mywindow (); } }   Hi Friend, There is no problem... mywindows.java Run : java mywindows Thanks RoseIndia Team
Java problem - Java Beginners
Java problem  I have an image in my application and I need to restrict the image path view on the browser. Noone should be able to right click and see the image path. Please help
Multiplication problem - Java Beginners
Multiplication problem  I am facing a peculiar problem in java regarding a multiplication. Please see below: 19300 * 0.001 = 19.3 19400 * 0.001 = 19.400000000000002 (why is this ??) 19500 * 0.001 = 19.5 Can anybody help
java problem - Java Beginners
java problem  Write a program to model a simple calculator. Each data... to power of right operand) q quit Your calculator should display the accumulator... is 0.23999999999999985 + 10 Result so far is 10.24 q 0 Final result is 10.24 
Java Problem - Java Beginners
Java Problem  How to create executable file of a java program.That is steps to create a Jar File of a Java Program  Hi Friend, Try the following code: import java.io.*; import java.util.jar.*; public class
java problem - Java Beginners
java problem  Suppose list is an array of five components of the type int.What is stored in list after the following Java code executes? for (i = 0; i < 5; i++) { list[i] = 2 * i + 5; if (i % 2 == 0) list[i] = list[i
JAVA Problem - Java Beginners
JAVA Problem  please help me!!!! How can i code in java using Two-dimensional Arrays? 88 90 94 102 111 122 134 75 77 80 86 94 103 113 80 83 85 94 100 111 121 68 71 76 85 96 110 125 77 84 91 98 105 112 119 81 85 90 96 102
java problem - Java Beginners
java problem  a) Binary numbers is important in computers. Binary number system uses only two digits, 0 and 1. There are pre-defined methods available in Java that will allow us to convert from denary integer (base 10
Java Problem - Java Beginners
Java Problem  How to create a jar file of a program  Hi Friend, Try the following code: import java.io.*; import java.util.jar.*; public class CreateJar { public static int buffer = 10240; protected void
java problem - Java Beginners
java problem  Hotel.java This file declares a class of object which maintains a collection of Room objects. It will contain methods which enable... from a small Java program
code problem - Java Beginners
java script j2ee j2me sql plz help me to sort out this problem. thnx  ...code problem  Dear sir, I'm havin a problem that suppose i've got a file that contains the following lines- java java script j2ee php sql
java problem - Java Beginners
java problem  Room.java This file defines a class of Room objects. The objects have the following instance variables:  number of beds in the room... from a small Java program. HotelMain.java The aim of this class is to provide
java problem - Java Beginners
java problem  Write a program that could be used to help children...(); System.out.print("How many problems would you like? "); double q = scan.nextDouble(); System.out.println(); double r=0,w=0; for (int idx = 1; idx <= q
Problem on JAVA Programme
Problem on JAVA Programme  public class AA { int add(int i) { int y = i; y += 20; if (y <= 100){ y +=30;add(y);} System.out.println("Final Value of y : " + y); return y; } public static void main
JAVA CLASSPATH PROBLEM
JAVA CLASSPATH PROBLEM  hi all Friends I am stuck using the java servlets and problem raise for classpath. I had a problem with servlet to call... that it didn't found any java class (which is java class calling from servlet). but i
java programming problem - JDBC
java programming problem  Hi, Request you to provide a solution... problem to the following mail id : Problem : upload excel file data into oracle database using java / j2ee. mail id : raichallp@yahoo.co.in
Problem in java 1.6 - Java Beginners
Problem in java 1.6  Am facing problem in java 1.6 . Ex. In a Frame......  Hi friend, Give source code where you having the problem For read more information on java visit to : http://www.roseindia.net/java
VoIP Implementation
VoIP Implementation VoIP Implementation Tour We?re packing our... is launching the Voice on VOIP Implementation Tour 2004-one-day workshops...-telephony is essentially a non-starter.    VoIP Implementation
code problem - Java Beginners
; Hi friend, Code to help in solving the problem : import java.io.... in Java visit to : http://www.roseindia.net/java/example/java/io/ Thanks
Basic problem for Java experts
Basic problem for Java experts  This assignment will test your knowledge of Arrays Array searching Array sorting Array processing Specification An athletics club require a simple statistical analysis program for analysing lap
code problem - Java Beginners
code problem  Dear sir, I have an excel file in D: drive called today.xls, i want to open it thru java program, what code would be compatible plz help me  Hi friend, Code to help in solving the problem : import
problem 1 - Java Beginners
problem 1   Hi, please help me!!!! How can i code in java using Two-dimensional Arrays? This question is related to the one i posted before. this is my input data file: 88 90 94 102 111 122 134 75 77 80 86 94 103 113 80
problem with main - Java Beginners
problem with main   import javax.swing.*; import java.awt.... a problem. when i compile it appears this message: java.lang.NoSuchMethodError: main... it with html file. applet.html: Java Applet Demo Thanks
Bid Problem - Java Beginners
in this application explain in details : Code to help in solving the problem...!"); } } } For more information on Java visit to : http://www.roseindia.net/java/ Thanks
java programming problem - Java Beginners
java programming problem   Given a deck of ncards unique cards.Cut the deck iCut cards from the top portion of the deck followed by the bottom card... etc. Alternating the remaining cards go on top. The problem is to find
problem - Java Beginners
in java if want something like this using array?   Hi friend, Code to solve the problem : import java.io.*; public class JavaMeanDeviation
problem - Java Beginners
in java if want something like this using array?   Hi friend, Code to solve the problem : import java.io.*; public class JavaMeanDeviation
code problem - Java Beginners
code problem  Dear sir, my problem is that I've a string value if this String value has "quit" then output should be "bye". i want to make this program using SWITCH CASE statement. how to implement String value in Switch plz
 

 

 

DMCA.com