Results 1 to 8 of 8

Thread: Threads

  1. #1
    GoLuM83 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Threads

    Good evening.

    It's possible to create a Thread with a loaded class.

    can i set something like

    new Thread("T1").setConstextClass("PersonalClass");

    Thanks in advace

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by GoLuM83 View Post
    Good evening.

    It's possible to create a Thread with a loaded class.

    can i set something like

    new Thread("T1").setConstextClass("PersonalClass");

    Thanks in advace
    The name is "setContextClassLoader"; you can set a class loader for a certain Thread; that class loader is used by everything that needs to load a class or a resource running in that particular class. Most applications don't need it.

    kind regards,

    Jos

  3. #3
    GoLuM83 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Threads

    Thanx for the answer i think that i did not understand the answer or that, my question was not well done.

    How can i tell a thread what run metod or class its going to use. I want to tell the thread the class with a string.

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by GoLuM83 View Post
    How can i tell a thread what run metod or class its going to use.
    If I understand you correctly, you simply pass the desired run method into the Thread's constructor.

    I want to tell the thread the class with a string.
    This confuses me. Perhaps you're looking at using reflection here?

  5. #5
    GoLuM83 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Default Threads

    Sorry for my english or explanations.

    Let me see i have 2 classes.

    Class A --> name "ClassA"
    Class B --> name "ClassB"

    another Class with main

    I only Know the String name of The class

    I know that i can create a thread with the constructor of each class but thats not what i want.

    I need to tell a thread.

    Thread T1 = new Thread();

    T1 --> use("ClassA");
    T2 --> use("ClassB");

    Its an example i know that use() dosn't exist.

    Thanx for your time =D

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again, reflection with a class loader could solve this. Or perhaps a HashMap<String, Runnable>. Why this requirement? What's the over-all problem that you're trying to solve. Likely the details would help us.

  7. #7
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    For that to work, T1 and T2 must implement Runnable.

    Java Code:
    try {
      Class c1 = Class.forName ("com.yourname.T1");
      Object o1 = c1.newInstance ();
      Runnable r1 = (Runnable)o1;
      Thread t1 = new Thread (r1);
      t1.start ();
    } catch (.. all kinds of exceptions...) {
    }

  8. #8
    GoLuM83 is offline Member
    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    Thumbs up Threads [Solved]

    Thanx iluxa, Fubarable, JosAH for your answers.

    Thats what i was looking for, the union of all solved my problem.

    :D

Similar Threads

  1. When to use threads
    By simorgh in forum Threads and Synchronization
    Replies: 2
    Last Post: 02-12-2010, 07:43 AM
  2. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 08:39 PM
  3. Threads!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 05-04-2008, 04:11 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:25 AM
  5. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:15 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •