Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training  
 

Threads

Ads

 
 

Share on Google+Share on Google+

manju
Threads
2 Answer(s)      9 years ago
Posted in : Java Beginners
Ads
View Answers

June 3, 2008 at 6:43 PM


Hi manju


import java.awt.*;
import java.applet.*;
import java.net.*;

public class AppletThread extends Applet {
private Image[] images;
private String[] text;

private Label captions;
private volatile int curFrame;
private Thread timerThread;
private volatile boolean noStopRequested;
private boolean paused;
private final Object pauseLock = new Object();

private void printThreadName(String prefix) {
String name = Thread.currentThread().getName();
System.out.println(prefix + name);
}

public void init() {
images = new Image[2];
text = new String[2];
captions = new Label();
setLayout(new BorderLayout());
add(BorderLayout.SOUTH, captions);

Label name = new Label("Display the image by Claude Monet");
name.setAlignment(Label.CENTER);
add(BorderLayout.EAST, name);

URL image = null;
try {
image = new URL("/home/vinod/amarexamples:9090/" + "amarexamples/Threads/applet/");
}
catch (java.net.MalformedURLException ex) {
System.out.println("URL not found");
return;
}

images[0] = getImage(image, "rose-one.jpg");
images[1] = getImage(image, "rose.gif");
text[0] = "Display cat image";
text[1] = "Display rose image in a west";
printThreadName("init is ");
startThread();
}

private void startThread() {
paused = true;
noStopRequested = true;
Runnable r = new Runnable() {
public void run() {
runWork();
}
};
timerThread = new Thread(r, "Timer");
timerThread.start();
printThreadName("startThread is ");
}

private void stopThread() {
noStopRequested = false;
timerThread.interrupt();
printThreadName("stopThread is ");
}
private void runWork() { // note that this is private
printThreadName("run is ");
curFrame = 0;

try {
while(noStopRequested){
waitWhilePaused();
curFrame = (curFrame + 1) % images.length;
repaint();
Thread.sleep(1000);
}
} catch ( InterruptedException x ) {
Thread.currentThread().interrupt();
System.out.println("interrupt and return from run");
}
}

private void setPaused(boolean newPauseState) {
synchronized (pauseLock) {
if (paused != newPauseState) {
paused = newPauseState;
pauseLock.notifyAll();
}
}
}

private void waitWhilePaused() throws InterruptedException {
synchronized (pauseLock) {
while (paused) {
pauseLock.wait();
}
}
}

public void paint(Graphics g) {
update(g);
printThreadName("paint is ");
}

public void update(Graphics g) {
g.drawImage(images[curFrame], 0, 0, this);
captions.setText(text[curFrame]);
printThreadName("update is ");
}

public void start(){
setPaused(false);
printThreadName("start is ");
}

public void stop() {
setPaused(true);
printThreadName("stop is ");
}

public void destroy() {
stopThread();

for (int i = 0; i < images.length; i++) {
images[i].flush();
images[i] = null;
text[i] = null;
}

images = null;
text = null;
printThreadName("destroy is ");
}
}
--------------------------------


June 3, 2008 at 6:45 PM


<HTML>
<BODY>
<div align = "center">
<APPLET CODE = "AppletThread.class" WIDTH = "500" HEIGHT = "400"></APPLET>
</div>
</BODY>
</HTML>



------------------------------------------


Read for more information.

http://www.roseindia.net/java

Thanks

Amardeep

Ads









Related Tutorials/Questions & Answers:
threads
threads  what are threads? what is the use in progarmming
threads in java
threads in java  how to read a file in java , split it and write into two different files using threads such that thread is running twice
Advertisements
threads and events
threads and events  Can you explain threads and events in java for me. Thank you.   Java Event Handling Java Thread Examples
Java threads
Java threads  What are the two basic ways in which classes that can be run as threads may be defined
disadvantage of threads
is the disadvantage of threads?   hello, The Main disadvantage of in threads... disadvantage of Threads. Let?s discuss the disadvantages of threads. The global..., the other threads using the same memory location will be killed automatically
Sync Threads
Sync Threads  "If two threads wants to execute a synchronized method in a class, and both threads are using the same instance of the class to invoke the method then only one thread can execute the method at a time." This is what
threads in java
threads in java  iam getting that the local variable is never read in eclipse in main classas:: class Synex4{ public static void main(String args[]){ Test1 ob1=new Test1(); //local variable never read
Version of cornerstone-threads>cornerstone-threads-tutorial dependency
List of Version of cornerstone-threads>cornerstone-threads-tutorial dependency
Version of cornerstone-threads>cornerstone-threads-api dependency
List of Version of cornerstone-threads>cornerstone-threads-api dependency
Version of cornerstone-threads>cornerstone-threads-impl dependency
List of Version of cornerstone-threads>cornerstone-threads-impl dependency
Threads in realtime projects
Threads in realtime projects  Explain where we use threads in realtime projects with example
Coding for life cycle in threads
Coding for life cycle in threads  program for life cycle in threads
Synchronized Threads
Synchronized Threads       In Java, the threads are executed independently to each other. These types of threads are called as asynchronous threads. But there are two problems may
Version of cornerstone-threads-api>cornerstone-threads-api dependency
List of Version of cornerstone-threads-api>cornerstone-threads-api dependency
Version of cornerstone-threads-impl>cornerstone-threads-impl dependency
List of Version of cornerstone-threads-impl>cornerstone-threads-impl dependency
Version of cornerstone-threads-tutorial>cornerstone-threads-tutorial dependency
List of Version of cornerstone-threads-tutorial>cornerstone-threads-tutorial dependency
Examples on threads and mulithreading.....
Examples on threads and mulithreading.....  Is any good examples on threads and Mulithreading...   Hi Friend, Please visit the following link: Thread Tutorial Thanks
Explain about threads:how to start program in threads?
Explain about threads:how to start program in threads?  import...; Learn Threads   Thread is a path of execution of a program... more than one thread. Every program has at least one thread. Threads are used
java threads - Java Beginners
java threads  What are the two basic ways in which classes that can be run as threads may be defined
Artifacts of cornerstone-threads
List of Artifacts of cornerstone-threads maven depenency
Artifacts of cornerstone-threads-api
List of Artifacts of cornerstone-threads-api maven depenency
Artifacts of cornerstone-threads-tutorial
List of Artifacts of cornerstone-threads-tutorial maven depenency
Artifacts of cornerstone-threads-impl
List of Artifacts of cornerstone-threads-impl maven depenency
Synchronized Threads
Synchronized Threads       In Java, the threads are executed independently to each other. These types of threads are called as asynchronous threads. But there are two problems may
Synchronized Threads
Synchronized Threads       In Java, the threads are executed independently to each other. These types of threads are called as asynchronous threads. But there are two problems may