Sina.iRoid
یک شنبه 24 اسفند 1393, 09:28 صبح
سلام
دوستان اگر به برنامه زیر دقت کنید، من دو تا ترد دارم. یه ترد اصلی و یک ترد فرزند. ترد فرزند بدون هیچ مشکلی اجرا میشه اما تا وقتی که بلاک try ترد اصلی در حالت کامنت باشه. وقتی که بلاک try ترد اصلی و از حالت کامنت خارج می کنم، یه مکث خیلی طولانی (مکث تا پایان اجرای ترد اصلی) می کنه و بعد خروجی و نشون میده. یعنی وقتی خروجی و نشون میده اجرای ترد ها تمام شده. لطفا راهنمایی کنید مشکل از کجاست. ممنون.
کلاس SecondThread:
import javax.swing.*;
import java.awt.*;
public class SecondThread extends JFrame implements Runnable{
private JLabel childTitleLabel;
private JLabel childLabel;
private JLabel mainTitleLabel;
private JLabel mainLabel;
private Thread t;
// Constructor
public SecondThread() {
setLayout(new FlowLayout());
childLabel = new JLabel();
childTitleLabel = new JLabel("Child thread: ");
// Create new, second thread
t = new Thread(this, "Demo Thread");
childLabel.setText(t.toString());
add(childTitleLabel);
add(childLabel);
t.start();
}
@Override
public void run() {
try {
for (int i = 0; i <= 10; i++) {
childLabel.setText(i + "");
t.sleep(500);
}
}catch (InterruptedException e){
childLabel.setText("Child thread interrupted.");
}
}
// getter and setter
public void setMainTitleLabel(JLabel mainTitleLabel) {
this.mainTitleLabel = mainTitleLabel;
}
public JLabel getMainTitleLabel() {
return mainTitleLabel;
}
public void setMainLabel(JLabel mainLabel){
this.mainLabel = mainLabel;
}
public JLabel getMainLabel(){
return mainLabel;
}
}
کلاس اصلی:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SecondThread ob = new SecondThread();
ob.setMainTitleLabel(new JLabel("Main Thread: "));
ob.add(ob.getMainTitleLabel());
// main thread
ob.setMainLabel(new JLabel());
ob.add(ob.getMainLabel());
try {
for (int i = 0; i <= 10; i++) {
ob.getMainLabel().setText(i + "");
Thread.sleep(1000);
}
}catch (InterruptedException e){
ob.getMainLabel().setText("maint hread interrupted.");
}
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(500, 500);
ob.setVisible(true);
}
}
ممنون میشم اگر راهنماییم کنید.
دوستان اگر به برنامه زیر دقت کنید، من دو تا ترد دارم. یه ترد اصلی و یک ترد فرزند. ترد فرزند بدون هیچ مشکلی اجرا میشه اما تا وقتی که بلاک try ترد اصلی در حالت کامنت باشه. وقتی که بلاک try ترد اصلی و از حالت کامنت خارج می کنم، یه مکث خیلی طولانی (مکث تا پایان اجرای ترد اصلی) می کنه و بعد خروجی و نشون میده. یعنی وقتی خروجی و نشون میده اجرای ترد ها تمام شده. لطفا راهنمایی کنید مشکل از کجاست. ممنون.
کلاس SecondThread:
import javax.swing.*;
import java.awt.*;
public class SecondThread extends JFrame implements Runnable{
private JLabel childTitleLabel;
private JLabel childLabel;
private JLabel mainTitleLabel;
private JLabel mainLabel;
private Thread t;
// Constructor
public SecondThread() {
setLayout(new FlowLayout());
childLabel = new JLabel();
childTitleLabel = new JLabel("Child thread: ");
// Create new, second thread
t = new Thread(this, "Demo Thread");
childLabel.setText(t.toString());
add(childTitleLabel);
add(childLabel);
t.start();
}
@Override
public void run() {
try {
for (int i = 0; i <= 10; i++) {
childLabel.setText(i + "");
t.sleep(500);
}
}catch (InterruptedException e){
childLabel.setText("Child thread interrupted.");
}
}
// getter and setter
public void setMainTitleLabel(JLabel mainTitleLabel) {
this.mainTitleLabel = mainTitleLabel;
}
public JLabel getMainTitleLabel() {
return mainTitleLabel;
}
public void setMainLabel(JLabel mainLabel){
this.mainLabel = mainLabel;
}
public JLabel getMainLabel(){
return mainLabel;
}
}
کلاس اصلی:
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SecondThread ob = new SecondThread();
ob.setMainTitleLabel(new JLabel("Main Thread: "));
ob.add(ob.getMainTitleLabel());
// main thread
ob.setMainLabel(new JLabel());
ob.add(ob.getMainLabel());
try {
for (int i = 0; i <= 10; i++) {
ob.getMainLabel().setText(i + "");
Thread.sleep(1000);
}
}catch (InterruptedException e){
ob.getMainLabel().setText("maint hread interrupted.");
}
ob.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ob.setSize(500, 500);
ob.setVisible(true);
}
}
ممنون میشم اگر راهنماییم کنید.