#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6

    Multithreading in Java


    I'm trying to figure out how to add a second thread to the program so that it calculates and outputs the factorial of the argument as well as the sum. Here is the original code:

    Code:
    class Sum
    {
      private int sum;
    
      public int get() {
        return sum;
      }
    
      public void set(int sum) {
        this.sum = sum;
      }
    }
    
    class Summation implements Runnable
    {
      private int upper;
      private Sum sumValue;
    
      public Summation(int upper, Sum sumValue) {
        this.upper = upper;
        this.sumValue = sumValue;
      }
    
      public void run() {
        int sum = 0;
    
        for (int i = 0; i <= upper; i++)
          sum += i;
    
        sumValue.set(sum);
      }
    }
    
    public class Driver
    {
      public static void main(String[] args) {
        if (args.length != 1) {
          System.err.println("Usage Driver <integer>");
          System.exit(0);
        }
    
        if (Integer.parseInt(args[0]) < 0) {
          System.err.println(args[0] + " must be >= 0");
          System.exit(0);
        }
    
        // Create the shared object
        Sum sumObject = new Sum();
        int upper = Integer.parseInt(args[0]);
    
        Thread worker = new Thread(new Summation(upper, sumObject));
        worker.start();
    
        try {
          worker.join();
        } catch (InterruptedException ie) { }
        System.out.println("sum(" + upper + ") = " + sumObject.get());
      }
    }
    Here is my attempt:
    Code:
    /**
     * This program creates a separate thread by implementing the Runnable interface.
     *
     * To compile, enter
     *  javac Driver.java
     *
     * Figure 4.8
     *
     * @author Gagne, Galvin, Silberschatz
     * Operating System Concepts  - Seventh Edition
     * Copyright John Wiley & Sons - 2005.
     */
    
    
    class Sum
    {
    	private int sum;
    
    	public int get() {
    		return sum;
    	}
    
    	public void set(int sum) {
    		this.sum = sum;
    	}
    }
    
    class Summation implements Runnable
    {
    	private int upper;
    	private Sum sumValue;
    
    	public Summation(int upper, Sum sumValue) {
    		if (upper < 0)
    			throw new IllegalArgumentException();
    
    		this.upper = upper;
    		this.sumValue = sumValue;
    	}
    
    	public void run() {
    		int sum = 0;
    
    		for (int i = 0; i <= upper; i++)
    			sum += i;
    
    		sumValue.set(sum);
    	}
    }
    class Factorial implements Runnable
    {
    
     	int i;
    	public int fact(int x){
    	for(i=1;i=x-1;i++){
    	i=(i*(i+1));
    	this.i=i;
    	return i;
    	}
       }
    }
    
    public class Driver
    {
    	public static void main(String[] args) {
    		if (args.length != 1) {
    			System.err.println("Usage Driver <integer>");
    			System.exit(0);
    		}
    
    		
    		int index = Integer.parseInt(args[0]);
    		Factorial factObject = new fact(index);
    		Thread worker = new Thread(new fact(index));
    		worker.start();
    		try {
    			worker.join();
    		} catch (InterruptedException ie) { }
    		System.out.println("The factorial of " + index + " is " + factObject.get());
    	}
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    What happens when you compile and execute the code? Are there errors or problems?
    Please post the errors and/or explain the problems.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    39
    Rep Power
    6
    Originally Posted by NormR
    What happens when you compile and execute the code? Are there errors or problems?
    Please post the errors and/or explain the problems.
    I'm getting an incompatible type error, several cannot find symbol errors, and "Factorial is not abstract and does not override abstract method run() in Runnable."
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Can you post the full text of the error messages?
    several cannot find symbol
    That means the compiler can not find a definition for the label/symbol shown in the error message.
    Factorial is not abstract and does not override abstract method run() in Runnable
    The Factorial class needs to define/override the run() method because it implements Runnable
  8. #5
  9. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    One problem here is that aren't implementing the interface.

    Code:
    class Factorial implements Runnable
    {
    
     	int i;
    	public int fact(int x){
    	for(i=1;i=x-1;i++){
    	i=(i*(i+1));
    	this.i=i;
    	return i;
    	}
       }
    }
    Also you have some simple syntax errors in your for loop.

    More important issues are here.

    Code:
    Factorial factObject = new fact(index);
    		Thread worker = new Thread(new fact(index));
    Last edited by bullet; October 24th, 2013 at 10:20 AM.

IMN logo majestic logo threadwatch logo seochat tools logo