Java   View all facts   Glossary   Help
object > thread
Next objectuninitialized object    Upobject    Previous objectstring   

thread
(lightweight process)
subjectfact 
threadis a synonym of lightweight process2001-10-19 11:37:16.0
belongs to a thread group which places limitations on the thread to protect it from other threadsadded by: JK, source: the Java Programming Language, 2001-10-19 11:38:06.0
can be blocked if it is waiting for or executing another function or thread that is blockedadded by: JK, source: the Java Programming Language, 2001-10-19 11:38:06.0
can be paused for a specific amount of time using the sleep methodadded by: JK, source: On To Java, 2001-10-19 11:38:06.0
can run concurrently with 1 or more other threadsadded by: JK, source: Sun Java Tutorial, 2001-10-19 11:38:06.0
has definition An object used in multithreaded software systems that represents a sequence of program executionadded by: JK, reference: Source 2, 2001-10-19 11:38:06.0
has thread priorityadded by: JK, source: the Java Programming Language, 2001-10-19 11:38:06.0
has example
public class ThreadExample implements Runnable
{
private int counterNumber; // Identifies the thread
private int counter; // How far the thread has executed
private int limit; // Where counter will stop
private long delay; // Pause in execution of thread in milisecs

// Constructor
private ThreadExample(int countTo, int number, long delay)
{
counter = 0;
limit = countTo;
counterNumber = number;
this.delay = delay;
}

//The run method; when this finishes, the thread terminates
public void run()
{
try
{
while (counter <= limit)
{
System.out.println("Counter "
+ counterNumber + " is now at " + counter++);
Thread.sleep(delay);
}
}
catch(InterruptedException e) {}
}

// The main method: Executed when the program is started
public static void main(String[] args)

//Create 3 threads and run them
Thread firstThread = new Thread(new ThreadExample(5, 1, 66));
Thread secondThread = new Thread(new ThreadExample(5, 2, 45));
Thread thirdThread = new Thread(new ThreadExample(5, 3, 80));

firstThread.start();
secondThread.start();
thirdThread.start();
}
}
  
source: Object Oriented Software Engineering by Lethbridge and Laganière, 2001-10-19 11:38:06.0
has purpose to enable a program to appear to be doing more than one thing at onceadded by: JK, source: On To Java, 2001-10-19 11:38:06.0
has purpose to perform a job such as waiting for events or performing a time-consuming job that the program doesn't need to complete before going onadded by: JK, source: Sun Java Tutorial, 2001-10-19 11:38:06.0
is a subtopic of Threads2001-10-19 11:38:06.0
is supported by many operating systems and programming languages    source: Object Oriented Software Engineering by Lethbridge and Laganière, 2001-10-19 11:38:06.0
is terminated when it has finished its jobadded by: JK, source: Sun Java Tutorial, modified by: DS, 2001-10-19 11:38:06.0
is a kind of objectadded by: JK, 2001-10-19 11:38:06.0
see also multithreading2001-10-19 11:38:06.0
see also synchronizationadded by: JK, 2001-10-19 11:38:06.0
see also Thread classsource: Object Oriented Software Engineering by Lethbridge and Laganière, 2001-10-19 11:38:06.0
stops permanently when its run method returnsadded by: JK, source: the Java Programming Language, 2001-10-19 11:38:06.0
to create you
  1. Create a class to contain the code that will control the thread. This can be made a subclass of Thread class, or else it can implement the interface Runnable.
  2. Write a method called run in your class. The run method will normally take a reasonably long time to execute - perhaps it waits for input in a loop. When the thread is started, this run method executes, concurrently with any other thread in the system. When the run method ends, the thread terminates
  3. Create an instance of Thread class, or your subclass of Thread class, and invoking the start operation on this instance. If you implemented the interface Runnable, you have to pass an instance of your class to the constructor of Thread class.
source: Object Oriented Software Engineering by Lethbridge and Laganière, 2001-10-19 11:38:06.0
object
when an object is created
  1. the object's variables are set to default values
  2. the constructor of the object's superclass is invoked
  3. the object's initialization block is invoked
  4. the object's constructor is invoked
2001-10-19 11:37:31.0
2001-10-19 11:37:31.0
2001-10-19 11:37:31.0
  • when an object is not needed any longer
  • the object's space is garbage collected
2001-10-19 11:37:31.0
can access all instance variables of all objects of its class2001-10-19 11:37:31.0
can be encapsulated by providing accessor methods2001-10-19 11:37:31.0
can be converted into a string using the toString method    2001-10-19 11:37:31.0
can be referred to without reference to the instance variables contained in it    2001-10-19 11:37:31.0
can be referred to by several different variables at the same time    2001-10-19 11:37:31.0
can communicate with every object to which it has access through its public interface2001-10-19 11:37:31.0
can convert itself to a string representation using the toString method    2001-10-19 11:37:31.0
can model    2001-10-19 11:37:32.0
can notify other objects that a condition variable has changed2001-10-19 11:37:32.0
can receive messages from    2001-10-19 11:37:32.0
can represent any entity to which you can associate properties and behaviour    2001-10-19 11:37:32.0
can return its class using the getClass method2001-10-19 11:37:32.0
can send messages to    2001-10-19 11:37:32.0
can wait on 0 or more condition variables2001-10-19 11:37:32.0
communicates with other objects by sending and receiving messages    2001-10-19 11:37:32.0
has example of creation String name = new String();2001-10-19 11:37:32.0
has part public interface2001-10-19 11:37:32.0
hides methods from other objects using the 'private' keyword2001-10-19 11:37:32.0
inherits instance methods and instance variables from
  • the class to which it belongs
  • all that class's superclasses
2001-10-19 11:37:32.0
is distinct from every other object even if they contain the same data    2001-10-19 11:37:32.0
is created by
  • declaring and initializing it such that it is created whenever execution enters a particular block, or, in the case of an instance variable, when an instance is created, for example:
    String aString = "some text";
  • or by declaring it and initializing it later using the new operator, for example:
    String aString;
    aString = new String("some text");
2001-10-19 11:37:32.0
shares every instance method of its class with the other instances of its class2001-10-19 11:37:32.0
shares its implementation with other instances of its class2001-10-19 11:37:32.0
shares one copy of each class variable of its class with the other instances of its class    2001-10-19 11:37:32.0

Next objectuninitialized object    Upobject    Previous objectstring