Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Hemming Our Way (Threads)

Custom Search
Hemming Our Way (Threads)

[ Return to Shlurrrpp......Java ]

Page: 1/5 



Next Page (2/5) Next Page
Hemming Our Way

Hemming Our Way

Hang on folks, here we have the programs dished out; the explanation will soon follow. Or for the people in the know, This page is under construction . A brief, clunky note about the programs is made to pass for an explanation, but we will be here soon (and hope you will be too!) with more explanatory stuff. As regards now, the programs will probably speak for themselves. :) Thanks for stopping by. See you soon. So long.


 


Consider this code...

 

1
zzz.java

import java.applet.*;
public class zzz extends Applet implements Runnable
{
}

That is not really the kind of code you can tear from limb to limb, but let us have a go. When we compile this code, an error called "class zzz must be declared abstract" pops up. This is because whenever we need a thread, we have to use implements Runnable. But a run() is also required.

 

2
import java.applet.*;
public class zzz extends Applet implements Runnable
{
	public void run()
         	{
            		showStatus("In Run");
         	}
}

The run() is specified here. The showStatus() does the work of showing whatever you pass as a parameter in statusbar of your window. Here when we execute, we only get the message "Applet started". The run() does not get called as yet. Now, ladies and gentlemen, we come to the concept of threads. To set the ball rolling (or more specifically, the run()), we need to have a thread. Because the Runnable ...runtime... So here we go.

 

 

3
import java.applet.*;
public class zzz extends Applet implements Runnable
{
         	Thread t;
         	public void init()
         	{
            		t  =  new Thread();
         	}
         	public void run()
         	{
            		showStatus("In Run");
         	}
}

Unless you've not been sleeping lately, you must have realised that we define an object t that looks like a Thread. In the init() we initialise the thread. After all this effort, the thread still does not start to weave! Well, we forgot to assossiate the thread with the run(). For doing this, we have a function called start() in the Thread class. Since t, our thread looks like Thread, we can use the start() by keying it in as t.start(). So here we go...

 

4
import java.applet.*;
public class zzz extends Applet implements Runnable
{
	Thread t;
 	public void init()
	{
		t  =  new Thread();
		t.start();
	}
	public void run()
	{
		showStatus("In Run");
	}
}

 

 

 

5
import java.applet.*;
public class extends Applet
{
	 Thread t;
	 public void init()
	{
		t  =  new Thread(this);
		t.start();
	}
	public void run()
	{
		showStatus("In Run");
	}
}

There, we finally have our first thread running and kicking. When we run the above program, we see "In Run" displayed at the bottom of our window. And like it or not, that is where the status is shown by default. Now that we can rest assured our thread works, let us make it do something for us. Retain the above code. In the run(), add the following lines..
 

	i++;
	repaint();

 

 

Now that we are calling the repaint(), we also have to have a paint(). So we include it as follows..

 

	public void paint(Graphics g)
	{
		g.drawString("i..." + i, 10, 20); 
	}

 

 

Now that we are using Graphics, we have to import java.awt.* . When you compile and run the program now, the output is sadly simplistic. The status, as before, drawls "In Run". At the coordinates 10, 20 we see "i...1", which is not that picturesque afterall. But wait, that explains one thing. When the run() gets called, the value of i is incremented by 1. So, i increases from 0 to 1. Now the repaint() calls the paint(). Here the drawString() puts "i...1" at 10, 20. That implies that the run() function gets called once, does its work and then the program does nothing further. To demonstrate this concept a little more clearly, let us make the run() do a little more work. Retaining the above code as it is, add the following lines to the run()...
 

 

		i++; repaint();
		i++; repaint();

 

 

This time, i is first displayed as 1, then 2 and then 3. This is because the repaint() is being called thrice. (In reality, though, you only see "i...3" owing to the speed of your computer).
How about putting the repaint() and the incrementing of i in a while loop? Remove all the i++ and repaint()s from your run() and put the following instead...
 

 

	while(true)
	{
		i++;
		repaint();
	}

 



[ Return to Shlurrrpp......Java ]


Top 10 read Java Articles
 Get free "1000 Java Tips eBook"

 Java Calendar and Date: good to know facts and code examples

 Array vs ArrayList vs LinkedList vs Vector: an excellent overview and examples

 How can I convert any Java Object into byte array? And byte array to file object

 The Java Lesson 1: What is Java?

 How do I compare two dates and times, date between dates, time between times and

 Maven vs Ant or Ant vs Maven?

 How to open, read, write, close file(s) in Java? Examples on move, rename and de

 Java Array

 Java: JLabel font and color


[ More in News Section ]
Java Lessons

The Java Lesson 1:
What is Java?
The Java Lesson 2:
Anatomy of a simple Java program
The Java Lesson 3:
Identifiers and primitive data types
The Java Lesson 4:
Variables, constants, and literals
The Java Lesson 5:
Arithmetic operations, conversions, and casts
The Java Lesson 6:
Boolean expressions and operations
The Java Lesson 7:
Bitwise operations
The Java Lesson 8:
Flow control with if and else
The Java Lesson 9:
switch statements
The Java Lesson 10:
for, while, and do-while statements
The Java Lesson 11:
Using break and continue
The Java Lesson 12:
Class methods and how they are called
The Java Lesson 13:
Using the Math class
The Java Lesson 14:
Creating and calling custom class methods
The Java Lesson 15:
Overloading class methods
The Java Lesson 16:
An introduction to objects and object references
The Java Lesson 17:
The String class
The Java Lesson 18:
The StringBuffer class
The Java Lesson 19:
Initializing and processing arrays of primitives
The Java Lesson 20:
Initializing and processing arrays of objects
The Java Lesson 23:
Inheritance and overriding inherited methods
The Java Lesson 24:
abstract classes and polymorphism
The Java Lesson 25:
Interfaces, instanceof, and object conversion and casting
The Java Lesson 26:
Introduction to graphical programming and the java.awt packa
The Java Lesson 27:
The Component class
The Java Lesson 28:
Containers and simple layout managers
The Java Lesson 29:
The Color and Font classes
The Java Lesson 30:
Drawing geometric shapes
The Java Lesson 31:
Choice, List, and Checkbox controls
The Java Lesson 32:
Using the Scrollbar graphical control
The Java Lesson 33:
Menus and submenus
The Java Lesson 34:
An introduction to applets and the Applet class
The Java Lesson 35:
Essential HTML to launch an applet and pass it parameters
The Java Lesson 36:
Mouse event processing
Java Lesson 37:
Menus and submenus
Java Lesson 38:
The WindowListener interface and the WindowAdapter class
Java Lesson 39:
An introduction to GridBagLayout
Java Lesson 40:
An introduction to the Java Collections API
Java Lesson 41:
Exception handling with try, catch, and finally blocks
Java Lesson 42:
Claiming and throwing exceptions
Java Lesson 43:
Multithreading, the Thread class, and the Runnable interface
Java Lesson 44:
An introduction to I/O and the File and FileDialog classes
Java Lesson 45:
Low-level and high-level stream classes
Java Lesson 46:
Using the RandomAccessFile class
Java Lessons by
Joh Huhtala: Update

Latest articles
 Java Profiler JProbe to Resolve Performance Problems Faster

 SSL with GlassFish v2, page 5

 SSL with GlassFish v2, page 4

 SSL with GlassFish v2, page 3

 SSL with GlassFish v2, page 2

 The Java Lesson 2: Anatomy of a simple Java program, page 2

 New site about Java for robots and robotics: both software and hardware.

 Exceptions -III: What's an exception and why do I care?

 Exceptions -II: What's an exception and why do I care?

 Exceptions: What's an exception and why do I care?

 Double your Java code quality in 10 minutes, here is receipt

 Murach's Java Servlets and JSP

 How to get ascii code from a char in Java?

 Can we just try without catch? Yes!

 Make Tomcat page load faster

 Make your Tomcat More secure - limit network address for certain IP addresses

 New Java book online starts now here...

 Implementing RESTful Web Services in Java

 Firefox trimming from 1 GB to 40 Mb with many tabs opened

 SSL with GlassFish v2

 My request to replublish Tech Tips

 Search JavaFAQ.nu site here

 New Advanced Installer for Java 6.0 brings XML updates and imports 3rd party MSI

 EJB programming restrictions

 Maven vs Ant or Ant vs Maven?

 Why Java does not use default value which it should?

 How to unsign signed bytes in Java - your guide is here

 The Java Lesson 3: Identifiers and primitive data types. Page 2

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 4

 The Java Lesson 7: Bitwise operations with good examples, click here! Page 3


[ More in News Section ]


Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy