954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

multithreading

Write a program to print * / * / * / * / * / * / * / using two child thread. One thread responsible for printing * and another for /

anjal_pawar
Newbie Poster
4 posts since Apr 2010
Reputation Points: 10
Solved Threads: 0
 

Okay. Finished.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Yup, me too.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

To program with threads you need to know how to answer these questions:

Where the Java thread support resides ?
When a tread executes, what code does it execute?
What states can a thread be in?


1- Where the Java thread support resides in:
* The java.lang.Thread class
* Certain parts of the java.lang.Object class
* Certain functionality of the Java language and runtime environment (e.g. the interface java.lang.Runnable).
And Every thread is an instance of the Thread class
2-When a tread executes, what code does it execute?
To cause a thread to execute, call its start() method. The start() method registers the thread with the thread scheduler.
What code is executed?
A thread executes the code in a public void run() method. There are two choices:
• The thread executes its own run() method
• The thread executes a run() method in some other object.
When the run() method returns, the thread dies.
The thread executes its own run method.
To do this, subclass the Thread class and override the run() method.
Example 1

public class PrintSlashThread extends Thread { 
public void run() { 
System.out.println("\"); 
}

Then, somewhere else in the program,

PrintSlashThread printSlashThread = new PrintSlashThread (); 
cs.start();

The thread executes another objects's run method
use a constructor with an argument which is a reference to an object of a class containing the run() method which also implements the Runnable interface.
The Runnable interface has a single method,

public void run().

Example 2

public class PrintSlashThread implements Runnable { 
public void run() { 
System.out.println("\"); 
}

Somewhere in a program (often in the constructor)

PrintSlashThread printSlashThread = new PrintSlashThread (); 
Thread tread = new Thread(printSlashThread); 
tread.start();


What states can a thread be in?
3-The thread states are:
Running: the goal state of all threads
Waiting states: Waiting, Sleeping, Suspended, Blocked
Ready: not waiting for anything except the CPU
Dead: all done
Now you can do put all together and post some proposition and we will tolk about synchronization
Hope it helps.

moutanna
Posting Whiz
387 posts since Oct 2009
Reputation Points: 16
Solved Threads: 58
 

Multithreading computers have hardware support to efficiently execute multiple threads. These are distinguished from multiprocessing systems (such as multi-core systems) in that the threads have to share the resources of a single core: the computing units, the CPU caches and the translation lookaside buffer (TLB). Where multiprocessing systems include multiple complete processing units, multithreading aims to increase utilization of a single core by leveraging thread-level as well as instruction-level parallelism. As the two techniques are complementary, they are sometimes combined in systems with multiple multithreading CPUs and in CPUs with multiple multithreading cores.

MaraShyla
Newbie Poster
3 posts since Apr 2010
Reputation Points: 7
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: