关于多线程

Che*_*tan 5 java multithreading

如何杀死线程?.....如何在多线程中重新启动它们?

Jon*_*ack 2

我将工作线程包装在它们自己的类中,并使用终止属性来终止线程过程循环。

抱歉,我现在手头没有 java 版本,但你应该从这个 http://pastie.org/880516中得到这个想法

using System.Threading; 

namespace LoaderDemo
{
    class ParserThread
    {
        private bool m_Terminated;
        private AutoResetEvent m_Signal;
        private string m_FilePath;
        ...

        public ParserThread(AutoResetEvent signal, string filePath)
        {
            m_Signal = signal; 
            m_FilePath = filePath;

            Thread thrd = new Thread(this.ThreadProc);
            thrd.Start(); 
        }

        public bool Terminated { 
            set { m_Terminated = value; } 
        }

        private Guid Parse(ref string s)
        {
            //parse the string s and return a populated Guid object
            Guid g = new Guid();

            // do stuff...

            return g;
        }

        private void ThreadProc()
        {
            TextReader tr = null;
            string line = null;
            int lines = 0;

            try
            {
                tr = new StreamReader(m_FilePath);
                while ((line = tr.ReadLine()) != null)
                {
                    if (m_Terminated) break;

                    Guid g = Parse(ref line);
                    m_GuidList.Add(g);
                    lines++;
                }

                m_Signal.Set(); //signal done

            }
            finally
            {
                tr.Close();
            }

        }

    }
}
Run Code Online (Sandbox Code Playgroud)