I have a class like,
Class A extends B {
String test = "";
String first = "";
public void testMethod() {
new Thread() {
public void run() {
testThreadMethod();
}
}.start();
}
public void testThreadMethod() {
System.out.println(first + " " + test);
}
}
The above class compiles fine. But in run time, the error is thrown in system.out.println() saying "invalid thread access".
Is there any wrong in the code. Accessing instance variables in multithread not allowed? Is there any way to access the instance variable inside the thread?
Thanks in advance.
EDITED NEW: To Reproduce the problem
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
public class SWTView extends ViewPart{
public static Display display;
public static Shell shell;
static Text sampleText;
static String testField1 = "";
static String firstField2 = "";
public static void main(String[] args) {
display = new Display();
shell = new Shell(display);
// Create a new Gridlayout with 2 columns
// where the 2 column do not have the
// same size
GridLayout layout = new GridLayout(2, false);
// set the layout of the shell
shell.setLayout(layout);
// Create a label and a button
sampleText = new Text(shell, SWT.NONE);
Label label = new Label(shell, SWT.NONE);
label.setText("A label");
Button button = new Button(shell, SWT.PUSH);
button.setText("Press Me");
// Create a new label that will spam two columns
label = new Label(shell, SWT.BORDER);
label.setText("This is a label");
// Create new layout data
GridData data = new GridData(GridData.FILL,
GridData.BEGINNING, true, false, 2, 1);
label.setLayoutData(data);
// Create a new label which is used as a separator
label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
// Create new layout data
data = new GridData(GridData.FILL, GridData.BEGINNING, true,
false, 2, 1);
data.horizontalSpan=2;
label.setLayoutData(data);
// Create a right aligned button
Button b = new Button(shell, SWT.PUSH);
b.setText("New Button");
b.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new Thread() {
public void run() {
printInstanceVariables();
}
}.start();
//showProgressBar() ---> // This is been implemented in another file which will shoe progress bar
}
});
data = new GridData(GridData.END, GridData.BEGINNING, false,
false, 2, 1);
b.setLayoutData(data);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public static void printInstanceVariables() {
System.out.println("Text entered is :: " +sampleText.getText());
System.out.println("test Field 1 is :: " + testField1);
System.out.println("first Field 2 is :: " + firstField2);
}
@Override
public void createPartControl(Composite arg0) {
// TODO Auto-generated method stub
}
@Override
public void setFocus() {
shell.setFocus();
}
}
The above code will throw invalid thread acesses ecpection @ printInstanceVariables() first system.out.println()
Answer: Got it.. It is because of accessing the component Text inside the thread in printInstanceVariables(). When i pass this component as paramter, everything works fine. Thanks for all you answers.