线程安全方法中的线程不安全对象

时间:2016-06-12 13:10:23

标签: java multithreading

我正在使用它:

window = tk.Tk()
window.title("Testwindow")
window.geometry("800x800")
window.configure(background='grey')

# images
im1_path = "im1.gif"
im2_path = "im2.gif"
im3_path = "im3.gif"
im1 = ImageTk.PhotoImage(Image.open(im1_path))
im2 = ImageTk.PhotoImage(Image.open(im2_path))
im3 = ImageTk.PhotoImage(Image.open(im3_path))

panel = tk.Label(window, image = im1)
panel.pack(side = "bottom", fill = "both", expand = "yes")

def callback(e):
    panel.configure(image = im2)
    panel.image = im2

    time.sleep(2)

    panel.configure(image = im3)
    panel.image = im3

window.bind("<Return>", callback)
window.mainloop()

由于@Override public synchronized void catchException(String logTag, Exception e) { StringBuilder trace = new StringBuilder(e.toString() + "\n"); for (StackTraceElement el : e.getStackTrace()) { trace.append("\t\t at " + el.toString() + "\n"); } write(logTag, trace.toString(), EXCEPTION); } 不是线程安全的,因此在线程安全的StringBuilder方法中使用它是否安全?我的猜测是,因为Object本身在方法中是本地的和线程安全的。

1 个答案:

答案 0 :(得分:4)

是的,使用StringBuilder是安全的,因为它对于方法来说是本地的,并且不会转义到任何其他线程。

相关问题