1

これは、自分のスレッドプールを作成する最初の試みです。すべてのスレッドを停止しようとした場合を除いて、プログラムは機能します。すべてのスレッドを中断しようとしていますが、何らかの理由でこのセグメントに到達しません。

if(this.currentThread()。isInterrupted())

これは出力です:

追加されたすべてのタスクタスク5 は
スレッド0によって受信されまし たタスク4はスレッド2によって受信されました これはタスク#4です タスク3はスレッド1によって受信されました これはタスク#3です すべて完了し、スレッドプールは閉じられました タスク5はスレッド0によって実行されています タスク2スレッド0によって受信されました これはタスク#2です タスク3は現在スレッド1によって実行されています タスク1はスレッド1によって受信されています これはタスク#1です タスク4は現在スレッド2によって実行されて います利用可能なタスクはありません... タスク1は現在スレッドによって実行されています1使用可能なタスクが ありません... タスク2がスレッドによって実行されています0 使用可能なタスクがありません...


















プログラムは、stopPool()メソッドが呼び出された後、出力の生成を停止することになっています。

ここで何が悪いのか誰か教えてもらえますか?

public class CustomThread extends Thread
{
            private int id;
            private Task task;
            private ThreadPool threadpool;

            public CustomThread(int id, Task task, ThreadPool threadpool)
            {
                this.id = id;
                this.task = task;
                this.threadpool = threadpool;
            }

            public synchronized void run()
            {
                while(true)
                {
                    if (this.currentThread().isInterrupted())
                    {
                        System.out.println("Thread " + id + " is halted");
                    }
                    else
                    {
                        if (threadpool.getTaskQueue().isEmpty())
                        {
                            System.out.println("No available tasks...");
                            try 
                            {
                                this.wait();
                            } 
                            catch (InterruptedException e) 
                            {
                                e.printStackTrace();
                            }
                        }
                        else
                        {   
                            task = threadpool.retrieveTask();
                            System.out.println("Task " + task.getID() + " recieved by thread " + id);
                            task.run();
                            task.setState(true);
                            System.out.println("Task " + task.getID() + " now being executed by thread " + id);
                        }
                    }
                }
            }

            public synchronized void wakeUp()
            {
                this.notify();
            }

            public int getThreadID()
            {
                return id;
            }

        }

    import java.util.List;
    import java.util.ArrayList;
    import java.util.Stack;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;


    public class ThreadPool
    {
        private List<CustomThread> thread_list;
        private Stack<Task> task_queue;

        public ThreadPool(int threads)
        {
            thread_list = new ArrayList<CustomThread>();
            task_queue = new Stack<Task>();
            for(int i=0; i<threads; i++)
            {
                CustomThread thread = new CustomThread(i, null, this);
                thread_list.add(thread);
                thread.start();
            }
        }

        //Must use notify() to wake up an idle thread
        public synchronized void add(Task task)
        {
            task_queue.push(task);
            try
            {
                for(CustomThread t : thread_list)
                {
                    t.wakeUp();
                }

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }

        }

        public synchronized void stopPool()
        {
            for(CustomThread t : thread_list)
            {
                t.currentThread().interrupt();
            }

        }

        public synchronized Stack getTaskQueue()
        {
            return task_queue;
        }

        public synchronized Task retrieveTask()
        {
            return task_queue.pop();
        }
    }

public class Task implements Runnable
{
    private int id;
    private boolean finished = false;
    public Task(int id)
    {
        this.id = id;
    }

    public synchronized void run()
    {
            System.out.println("This is task #" + id);

            try
            {
                Thread.sleep(1000);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    }

    public int getID()
    {
        return id;
    }

    public void setState(Boolean finished)
    {
        this.finished = finished;
    }

    public boolean getState()
    {
        return finished;
    }
}


public class Init 
{
    public static void main(String[] args)
    {
        ThreadPool threadpool = new ThreadPool(3);
        threadpool.add(new Task(1));
        threadpool.add(new Task(2));
        threadpool.add(new Task(3));
        threadpool.add(new Task(4));
        threadpool.add(new Task(5));
        System.out.println("All tasks added");
        {
            try
            {
                Thread.sleep(1000);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        threadpool.stopPool();
        System.out.println("All done, threadpool closed");
    }

}
4

1 に答える 1

3

あなたのコード

t.currentThread().interrupt();

tスレッドが代わりに試行するのではなく、現在のスレッドを繰り返し中断します

t.interrupt();

wait()ところで:私はあなたがログに記録し、それが起こらなかったかのように続行する例外をスローすることを期待します。


あなたの興味のために、これはあなたがExecutorServiceを使ってそれを書くかもしれない方法です。

ExecutorService service = Executors.newFixedThreadPool(3);
for (int i = 1; i <= 5; i++) {
    final int id = i;
    service.submit(new Callable<Void>() {
        @Override
        public Void call() throws Exception {
            System.out.println("This is task #" + id);
            Thread.sleep(1000);
            return null;
        }
    });
}
System.out.println("All tasks added");
service.shutdown();
service.awaitTermination(1, TimeUnit.HOURS);
System.out.println("All done, threadpool closed");

プリント

This is task #1
This is task #3
This is task #2
All tasks added
This is task #4
This is task #5
All done, threadpool closed
于 2012-10-02T10:54:30.033 に答える