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

  • SEARCH
  • Home
  • 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 8044179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:08:40+00:00 2026-06-05T05:08:40+00:00

To share TaskKey among Tasks, which one is better? Are there other better way?

  • 0

To share TaskKey among Tasks, which one is better? Are there other better way?

way1 share by a key object.In this way, The client code(Task Queue) is easier to write to config the task table. But every task has to waste memory to store the TaskKey object.

class TaskKey {
    int key1;
    int key2;
    // key3...
    TaskKey(int key1, int key2) {
        this.key1 = key1;
        this.key2 = key2;
    }
}

abstract class Task implements Cloneable {
    TaskKey key;
    int taskData;
    Task(TaskKey key) {
        this.key = key;
    }
    int getKey1() {
        return key.key1;
    }
    int getKey2() {
        return key.key2;
    }
    Task newInstance(int taskData) {
        Task task = (Task) clone();
        task.taskData = taskData;
        return task;
    }
    abstract void doSomething();
}

class TaskQueue {
    Task[][] taskTable;
    void addTask(Task task) {
        taskTable[task.getKey1()][task.getKey2()] = task;
    }
    void config() {
        addTask(new Task(new TaskKey(1, 1)) {
            void doSomething() {}
        });
        addTask(new Task(new TaskKey(1, 2)) {
            void doSomething() {}
        });
        addTask(new Task(new TaskKey(2, 1)) {
            void doSomething() {}
        });
    }

    Queue<Task> queue;
    void put(int key1, int key2, int taskData) {
        Task task = taskTable[key1][key2];
        queue.add(task.newInstance(taskData));
    }
}

way2 share by the task class.In this way, The client code(Task Queue) is troublesomely to write to config the task table(to override the getKey() method of Task). But every task do not need to waste memory to store the TaskKey object, because the key information are shared by the concrete Task class.

abstract class Task implements Cloneable {
    int taskData;
    abstract int getKey1();
    abstract int getKey2();
    abstract void doSomething();
    Task newInstance(int taskData) {
        Task task = (Task) clone();
        task.taskData = taskData;
        return task;
    }
}

class TaskQueue {
    Task[][] taskTable;
    void addTask(Task task) {
        taskTable[task.getKey1()][task.getKey2()] = task;
    }
    void config() {
        addTask(new Task() {
            int getKey1() { return 1; }
            int getKey2() { return 1; }
            void doSomething() {}
        });
        addTask(new Task() {
            int getKey1() { return 1; }
            int getKey2() { return 2; }
            void doSomething() {}
        });
        addTask(new Task() {
            int getKey1() { return 2; }
            int getKey2() { return 1; }
            void doSomething() {}
        });
    }

    Queue<Task> queue;
    void put(int key1, int key2, int taskData) {
        Task task = taskTable[key1][key2];
        queue.add(task.newInstance(taskData));
    }
}

another way1 and way2

interface TaskExecutor {
    void exec(Task task);
}

class TaskKey {
    int key1;
    int key2;
    int key3;
    TaskExecutor executor;
}

class Task /*another way1*/ {
    TaskKey key;
    int taskData;// in fact it is not only a int

    int getKey1() { // because i need to retrieve key1 from a task
        return key.key1;
    }
    // also get methods for key2 key3

    void exec(){
        key.executor.exec(this);
    }
}

class Task /*another way2*/ {
    public final int key1;
    public final int key2;
    public final int key3;
    private final TaskExecutor executor;
    int taskData;

    void exec(){
        executor.exec(this);
    }
}
  • 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-06-05T05:08:42+00:00Added an answer on June 5, 2026 at 5:08 am

    You probably don’t want to do it the second way. For something as simple as storing two ints, reflection seems to be a bit overkill. The amount of memory performance you get from this is very small, and jvm will have to work that much harder to figure out your method overloading. It’ll end up taking up a lot more code space (where do you think the java compiler stores all this extra information?) as well as slowing down execution.

    The first way is one way of doing it, but if it’s something as simple as storing two ints you might consider just having the two keys in your Task class instead of having a separate key.

    abstract class Task implements Cloneable {
        int taskData;
        int key1;
        int key2;
        Task(int key1_, int key2_) {
            this.key1 = key1_;
            this.key2 = key2_;
        }
        int getKey1() {
            return key1;
        }
        int getKey2() {
            return key2;
        }
        Task newInstance(int taskData) {
            Task task = (Task) clone();
        task.taskData = taskData;
            return task;
        }
        abstract void doSomething();
    }
    

    However, it seems like you don’t need to do this considering you already have a Task[][] storing all this data, unless if you need to get the values of key1 and key2 from a specific Task object, you can instead just directly do

    void addTask(Task task, int i, int j) {
        taskTable[i][j] = task;
    }
    void config() {
        addTask(new Task(), 1, 1);
    }
    

    However, if you DO need to get the values of key1 and key2 from a specific Task object, you can instead use a HashMap<TaskKey,Task> instead of a Task[][] to store your data. That way you can

    HashMap<TaskKey,Task> taskTable;
    void addTask(Task task, int i, int j) {
        taskTable.put(new TaskKey(i,j), task);
    }
    Task getTask(int i, int j){
        return taskTable.get(new TaskKey(i,j));
    }
    void config() {
        addTask(new Task(), 1, 1);
    }
    

    though you’d probably need to generate a hashCode() and equals() method using your favorite IDE for both of them. This ends up being very memory efficient, with the same constant time access you have from the array.

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

Sidebar

Related Questions

I want to share object of type Stuff which contains ( String name,address,title, ...
I want to share session cookie among domains. I have more than one domain:
How does one share common_fn() among all specializations (for Widget<A<T> > and Widget<B<T> >
Can you share the way you setup your unit test projects within your .net
I've got a Share on Facebook button which launches an ACTION_SEND intent so the
want to share a link from my application in windows phone 7. Any one
If I share an IORef among multiple threads, and use atomicModifyIORef to write to
I share a project with other team-members over SVN. Now I am using the
Can any one share the actual difference between WCF Service and ASP.NET Web Api?
If we want to share our data with other Android applications, (1) we may

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.