Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:21:46+00:00 2026-05-26T10:21:46+00:00

i have some small problem. I hava some class Processor and Process . Process

  • 0

i have some small problem.
I hava some class Processor and Process. Process class have only name and value
Processor must calculate this value and display it. In turn, Processor implement Runnable interface
But i have a trouble. When I set process to my Processor, it begin to calculate value, but i want to start another thread with different value of process, and this value set to first thread too.
Here is code

    package ua.edu.lp.controller;

import java.util.Random;

import ua.edu.lp.controller.processors.FirstProcessor;
import ua.edu.lp.controller.processors.MyBaseProcessor;
import ua.edu.lp.model.MyProcess;
import ua.edu.lp.model.Queue;

public class Computer {

    private Queue<MyProcess> queue;

    public void run() {
        MyBaseProcessor myBaseProcessor = new FirstProcessor();
        generateQueue();

        for (int i = 0; i < queue.getQueue().size(); i++) {
            MyProcess process = queue.get(i);
            myBaseProcessor.setProcess(process);
            Thread thread = new Thread(myBaseProcessor);
             thread.start();
        }
    }

    private void generateQueue() {
        queue = new Queue<MyProcess>();
        for (int i = 0; i < 10; i++) {
            String name = "Process[" + (i + 1) + "]";
            MyProcess myProcess = new MyProcess(name,
                    new Random().nextInt(10) + 1);
            queue.add(myProcess);
            System.out.println(myProcess.getName() + "\t"
                    + myProcess.getValue());
        }
    }
}

BaseProcessor

package ua.edu.lp.controller.processors;

import ua.edu.lp.model.MyProcess;

public abstract class MyBaseProcessor implements Runnable {

    protected MyProcess myProcess;

    @Override
    public void run() {
        synchronized (myProcess) {
            calculateValue();
        }
    }

    protected abstract void calculateValue();

    public synchronized MyProcess getProcess() {
        return myProcess;
    }

    public synchronized void setProcess(MyProcess myProcess) {
        synchronized (myProcess) {
            this.myProcess = myProcess;
        }

    }

}

And FirstProcessor

package ua.edu.lp.controller.processors;

public class FirstProcessor extends MyBaseProcessor {

    @Override
    protected synchronized void calculateValue() {
        System.out.println("First processor is working...");
        int value = this.myProcess.getValue();
        int result = (int) Math.pow(value, 2);
        System.out.println("Pow of " + value + " = " + result);
        System.out.println("First processor is free");
    }

}

All project you can download from http://www.sendspace.com/file/5rlmkx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:21:46+00:00Added an answer on May 26, 2026 at 10:21 am

    Make Processor stateless – remove field process and do something like this:

    final Processor processor = new Processor();
    
    processor.process(new Process(...));
    
    public void process( final Process process ) {
        new Thread(new Runnable() {
            //do somethig with process
            }).run();
    }
    

    NOTE: Processor must do only common task – to run process, and process implement business logic and only process aware of calculations you’ve done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While doing some small regex task I came upon this problem. I have a
I have some small problem in understanding Value Objects in Flex... I'm trying to
Hi i have a small problem , iam using join to get some names
i'm updating some VB6 code to .NET and have come across a small problem.
i have small problem understanding why my smart pointer class is leaking on self
I am attempting some pattern matching in Lua and have hit a small problem.
I have a small problem, I have some static content inside a div and
We have been facing a small problem in MySQL procedure. We have placed some
I have this script which works except for one small problem. Basically it gets
I have some troubles with this small JavaScript code: var text=Z Test Yeah !

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.