Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Thread and synchronized view

Status
Not open for further replies.

koolraul

Programmer
Aug 20, 2003
10
US
I declared my synchronized view as: Set tset = Collections.synchronizedSet(new TreeSet(new RectComparator()));. I created a thread class as: private class AddThread implements Runnable { ... }. Inside the thread constructor, I'm passing a LinkedList and a Set as parameters e.g., public AddThread (LinkedList list, Set set) { ... }. When I tried to create a thread as: Thread bthread = new Thread(new AddThread(list, tset)); I was getting the following error on compilation time: C:\..\Demo.java:83: non-static variable this cannot be referenced from a static context Thread bthread = new Thread(new AddThread(list,
tset));
^
Does the non-static variable 'this' refers to 'list' or to the Demo (class that contains the main())? Why would the variable 'list' cannot be reference from a static context? My linked list declaration is inside the main. Why is it considering the list as non-static if it was declared within the main? Eventhough I made my List and Set declarations private static..., I was still getting the same compiler message. Any help or suggestions are greatly appreciated. Thanks.
 
What I normally do to avoid this sort of problem :
Code:
public class Demo {

  public Demo() {
  }

  // non-static method
  public void doIt() {
    ... do whatever you want ...
  }

  public static void main(String[] args) {
    Demo demo = new Demo();
    demo.doIt();
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top