Java 为什么可以';我不能杀线吗?

Java 为什么可以';我不能杀线吗?,java,multithreading,Java,Multithreading,亲爱的所有人,我有这种类型的代码: public class Testimplements Runnable { public static void main(String[] args) { InTheLoop l= new InTheLoop(); Thread th = new Thread(l); th.start(); th.interrupt(); } @Override pub

亲爱的所有人,我有这种类型的代码:

public class Testimplements Runnable  {

    public static void main(String[] args) {

        InTheLoop l= new InTheLoop();
        Thread th = new Thread(l);
        th.start();
        th.interrupt();
    }

    @Override
    public void run() {

        int count = 0;
        for (Integer i = 0; i <=10000000000000000000; i++) { 
        }
    }
}
但是我不能在没有if语句的情况下终止线程吗?

如果确实需要,可以使用
stop()
方法,但要注意它本身是不安全的,不推荐使用。 有关详细信息,请参阅。

如果确实需要,您可以使用
stop()
方法,但请注意它本身是不安全的,不推荐使用。
有关详细信息,请参阅。

据我所知,您必须为自己的线程实施中断策略,它如何处理中断调用以及何时停止等


请参阅:

据我所知,您必须为自己的线程实现中断策略,它如何处理中断调用以及何时停止等


请参阅:

更好地使用以获得对线程的更多控制,而不是线程


阅读更多信息,这里是

,而不是线程更好地用于对线程进行更多控制


阅读更多,这里是

这个主题非常重要,我找不到任何明确的答案,所以我用OOP表单编写了一个示例代码来解释这一点

import threading
import time
import tkinter as tk


class A(tk.Tk):
    def __init__(self ,*args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.count = 0
        self._running = True
        self.Do = 0
        A.Run(self)

    def Thread_Terminate(self):                     # a method for killing the Thread
        self._running =False
        self.T.join()

        self.Btn2.config (state='normal')
        self.Btn1.config (state='disabled')

    def Thread_Restart(self):                       # a thred for Start and Restart the thread
        self.Btn1.config (state='normal')
        self._running = True

        self.T = threading.Thread(target=self.Core) # define Thread

        if (self.T.is_alive() != True):             # Cheak the Thread is that alive
            self.T.start()

        self.Btn2.config (state='disabled')

    def Window (self):
        self.title(" Graph ")
        self.geometry("300x300+100+100")
        self.resizable(width=False, height=False)

        self.Lbl1 = Label(self,text = '0' ,font=('Times', -50, 'bold'), fg='Blue')
        self.Lbl1.pack()
        self.Lbl1.place(x=130, y=30 )

        self.Btn1 = Button(self,text= 'Thread OFF',width=25,command = self.Thread_Terminate)
        self.Btn1.pack()
        self.Btn1.place(x=50,y=100)
        self.Btn1 ['state'] = 'disable'

        self.Btn2 = Button(self, text='Thread ON', width=25, command=self.Thread_Restart)
        self.Btn2.pack()
        self.Btn2.place(x=50, y=140)

        self.Ent1 = Entry(self, width  = 30, fg='Blue')
        self.Ent1.pack()
        self.Ent1.place(x=50, y=180)

    def Core(self):                                 # this method is the thread Method
        self.count = 0
        i = self.Ent1.get()
        if (i==''):
            i='10'

        while (self._running and self.count<int(i)):
            self.count +=1

            self.Lbl1.config(text=str(self.count))

            print(str (self.count)+'Thread Is ON')
            time.sleep(0.5)

    def Run(self):
        A.Window(self)
        self.mainloop()


if __name__ == "__main__":
    Obj1 = A()
导入线程
导入时间
将tkinter作为tk导入
A类(tk.tk):
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
self.count=0
self.\u running=True
self.Do=0
跑步(自我)
def Thread_Terminate(self):#终止线程的方法
self.\u running=False
self.T.join()
self.Btn2.config(state='normal')
self.Btn1.config(state='disabled')
def Thread_Restart(self):#用于启动和重新启动线程的thred
self.Btn1.config(state='normal')
self.\u running=True
self.T=threading.Thread(target=self.Core)#定义线程
如果(self.T.is_alive()!=True):#检查线程是否处于活动状态
self.T.start()
self.Btn2.config(state='disabled')
def窗口(自):
标题(“图表”)
自几何(“300x300+100+100”)
可自行调整大小(宽度=假,高度=假)
self.Lbl1=标签(self,text='0',font=('Times',-50',bold'),fg='Blue')
self.Lbl1.pack()
自身Lbl1位置(x=130,y=30)
self.Btn1=按钮(self,text='Thread OFF',width=25,command=self.Thread\u Terminate)
self.Btn1.pack()
自身Btn1位置(x=50,y=100)
self.Btn1['state']='disable'
self.Btn2=按钮(self,text='Thread ON',width=25,command=self.Thread\u Restart)
self.Btn2.pack()
自身Btn2位置(x=50,y=140)
self.Ent1=入口(self,宽度=30,fg='Blue')
self.Ent1.pack()
self.Ent1.place(x=50,y=180)
def Core(self):#此方法是线程方法
self.count=0
i=self.Ent1.get()
如果(i=''):
i='10'

当(self._运行和self.count时,这个主题非常重要,我找不到任何明确的答案,所以我用OOP表单编写了一个示例代码来解释这一点

import threading
import time
import tkinter as tk


class A(tk.Tk):
    def __init__(self ,*args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.count = 0
        self._running = True
        self.Do = 0
        A.Run(self)

    def Thread_Terminate(self):                     # a method for killing the Thread
        self._running =False
        self.T.join()

        self.Btn2.config (state='normal')
        self.Btn1.config (state='disabled')

    def Thread_Restart(self):                       # a thred for Start and Restart the thread
        self.Btn1.config (state='normal')
        self._running = True

        self.T = threading.Thread(target=self.Core) # define Thread

        if (self.T.is_alive() != True):             # Cheak the Thread is that alive
            self.T.start()

        self.Btn2.config (state='disabled')

    def Window (self):
        self.title(" Graph ")
        self.geometry("300x300+100+100")
        self.resizable(width=False, height=False)

        self.Lbl1 = Label(self,text = '0' ,font=('Times', -50, 'bold'), fg='Blue')
        self.Lbl1.pack()
        self.Lbl1.place(x=130, y=30 )

        self.Btn1 = Button(self,text= 'Thread OFF',width=25,command = self.Thread_Terminate)
        self.Btn1.pack()
        self.Btn1.place(x=50,y=100)
        self.Btn1 ['state'] = 'disable'

        self.Btn2 = Button(self, text='Thread ON', width=25, command=self.Thread_Restart)
        self.Btn2.pack()
        self.Btn2.place(x=50, y=140)

        self.Ent1 = Entry(self, width  = 30, fg='Blue')
        self.Ent1.pack()
        self.Ent1.place(x=50, y=180)

    def Core(self):                                 # this method is the thread Method
        self.count = 0
        i = self.Ent1.get()
        if (i==''):
            i='10'

        while (self._running and self.count<int(i)):
            self.count +=1

            self.Lbl1.config(text=str(self.count))

            print(str (self.count)+'Thread Is ON')
            time.sleep(0.5)

    def Run(self):
        A.Window(self)
        self.mainloop()


if __name__ == "__main__":
    Obj1 = A()
导入线程
导入时间
将tkinter作为tk导入
A类(tk.tk):
定义初始化(self,*args,**kwargs):
tk.tk.\uuuuu初始化(self,*args,**kwargs)
self.count=0
self.\u running=True
self.Do=0
跑步(自我)
def Thread_Terminate(self):#终止线程的方法
self.\u running=False
self.T.join()
self.Btn2.config(state='normal')
self.Btn1.config(state='disabled')
def Thread_Restart(self):#用于启动和重新启动线程的thred
self.Btn1.config(state='normal')
self.\u running=True
self.T=threading.Thread(target=self.Core)#定义线程
如果(self.T.is_alive()!=True):#检查线程是否处于活动状态
self.T.start()
self.Btn2.config(state='disabled')
def窗口(自):
标题(“图表”)
自几何(“300x300+100+100”)
可自行调整大小(宽度=假,高度=假)
self.Lbl1=标签(self,text='0',font=('Times',-50',bold'),fg='Blue')
self.Lbl1.pack()
自身Lbl1位置(x=130,y=30)
self.Btn1=按钮(self,text='Thread OFF',width=25,command=self.Thread\u Terminate)
self.Btn1.pack()
自身Btn1位置(x=50,y=100)
self.Btn1['state']='disable'
self.Btn2=按钮(self,text='Thread ON',width=25,command=self.Thread\u Restart)
self.Btn2.pack()
自身Btn2位置(x=50,y=140)
self.Ent1=入口(self,宽度=30,fg='Blue')
self.Ent1.pack()
self.Ent1.place(x=50,y=180)
def Core(self):#此方法是线程方法
self.count=0
i=self.Ent1.get()
如果(i=''):
i='10'

(self._正在运行,self.count当然不是。线程必须知道对它做了什么,并且必须采取明确的措施以安全的方式结束。没有“进程隔离”的等价物对于线程来说,如果你突然杀死一个线程,整个JVM就会不稳定。当然不是。线程必须知道对它做了什么,并且必须采取明确的措施以安全的方式结束。线程没有“进程隔离”的等价物,因此如果你突然杀死一个线程,整个JVM就会不稳定。