|
-
Sep 25th, 2001, 11:04 AM
#1
Thread Starter
Hyperactive Member
running threads [resolved]
I've look at all the API docs and tutorials I can find and I still don't know how to start a thred.
I know (correct me if I'm wrong please) that to create something that can be used as a thread it must either extend Thread or implement Runnable and then override the run() method with the code that you would normally put in a main....
Last edited by CaptainPinko; Oct 22nd, 2001 at 10:30 AM.
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Sep 25th, 2001, 11:12 AM
#2
Here's a simple thread example:
Code:
class Athread implements Runnable
{
void run()
{
for(int x=0;x<10;x++)
{
System.out.println(x);
Thread.sleep(200);
}
}
}
class Aprog
{
Thread t = new Athread();
t.start();
}
that should work, but I haven't used threads in a few weeks, so it may be buggy.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Sep 25th, 2001, 11:35 AM
#3
Dazed Member
Here is a little sample code for you to look at. There are a couple of ways to multithread in Java. You can either subclass thread
or declare a class that implemlemts a Runnable interface and pass an instance of that class to the Threads constructor.
If you want to multithread in the same class you could do somthing like this.
Code:
public class ThreadTest implements Runnable{
private Thread worker;
public ThreadTest(){
worker = new Thread(this, threadname)
worker.start();
}
public void run(){
// code...............
}
}
It's normally perfered to not subclass the Thread class because the subclass will not be able to extends other classes so this is somthing to consider.
Some sample code............
Code:
class Storage{
int value;
synchronized void setValue(int i){value = i;}
synchronized int getValue(){return value;}
}
class Printer implements Runnable{
Thread t1;
Storage store;
public Printer(Storage target){
store = target;
t1 = new Thread(this);
t1.start();
}
public void run(){
try{
while(true){
System.out.println("The value of storage is" + store.getValue());
t1.sleep(1000); // 1 second of sleep
}
}catch(Exception e){System.err.println(e);}
}
}
class Counter implements Runnable{
Thread t2;
int i;
Storage storage;
public Counter(Storage target){
storage = target;
t2 = new Thread(this);
t2.start();
}
public void run(){
try{
while(true){
storage.setValue(i);
++i;
t2.sleep(1000); // 1 second of sleep
}
}catch(Exception e){System.err.println(e);}
}
}
public class Client{
public static void main(String[] args){
Storage store = new Storage();
new Counter(store);
new Printer(store);
}
}
-
Sep 26th, 2001, 12:54 PM
#4
Dazed Member
-
Oct 13th, 2001, 02:02 PM
#5
Thread Starter
Hyperactive Member
CrptcBlade your example doesn't work, i tried this
Code:
public class ReadTester {
public static void main (String args[]) {
Thread a = new BigMe();
}
}
class BigMe implements Runnable {
public void run () {for (int i = 0; i < 10; i++) System.out.println ("Big Me");}
}
and got the JAVAC error:
D:\Java\ReadTester.java:3: incompatible types
found : BigMe
required: java.lang.Thread
Thread a = new BigMe();
^
and Dilenger4, your example is quite cluttered and i can't really see what you are doing
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Oct 13th, 2001, 02:05 PM
#6
Member
Originally posted by crptcblade
Here's a simple thread example:
Code:
class Athread implements Runnable
{
void run()
{
for(int x=0;x<10;x++)
{
System.out.println(x);
Thread.sleep(200);
}
}
}
class Aprog
{
Thread t = new Athread();
t.start();
}
that should work, but I haven't used threads in a few weeks, so it may be buggy.
Two classes in one .java file = big no-no.
-
Oct 13th, 2001, 02:09 PM
#7
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 13th, 2001, 02:15 PM
#8
Thread Starter
Hyperactive Member
filburt1, God of cut'n'paste, that code DOESN"T work as I said so *** was the purpose of posting it again?
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Oct 13th, 2001, 02:33 PM
#9
Member
I was just quoting him.
-
Oct 13th, 2001, 02:40 PM
#10
Thread Starter
Hyperactive Member
<smacks head into monitor>for what purpose? to increase peoples' pleasure of scrolling?!?
Last edited by CaptainPinko; Oct 13th, 2001 at 07:18 PM.
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Oct 13th, 2001, 02:41 PM
#11
working example :
Code:
public class TestThreads
{
public static void main(String[] args)
{
Thread t = new Thread(new ThreadTest());
t.start();
}
}
class ThreadTest implements Runnable
{
public void run()
{
for (int x=0; x<10; x++)
{
System.out.println(x);
try
{
Thread.sleep(500);
}
catch(InterruptedException e){}
}
}
}
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Oct 13th, 2001, 04:29 PM
#12
PowerPoster
Originally posted by filburt1
Two classes in one .java file = big no-no.
there is nothing wrong with doing that.
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Oct 13th, 2001, 07:21 PM
#13
Dazed Member
Hey, at least my code runs.
-
Oct 13th, 2001, 10:19 PM
#14
Dazed Member
The code i posted was to show what
happens when synchronization is used
while in the context of "multi thredding."
The code really isnt that hard to understand.
You have two classes that implement a runnable
interface, which inturn overide the run() method.
Here you are creating a instance of the Storage
class which is stored as a refrence in store.
For Counter and Printer you are creating two
instances but your just not storing them within
a refrence variable.
Code:
public class Client{
public static void main(String[] args){
Storage store = new Storage();
new Counter(store);
new Printer(store);
}
}
All this class is doing is setting the value
of the integer variable "i" in the Storage class.
one of the Thread class constructors is public
Thread(Runnable target) Since you are creating
a thread within the same class that
is implementing the Runnable interface you just
pass in "this" to the Thread constructor, as we have done.
Code:
class Counter implements Runnable{
Thread t2;
int i;
Storage storage;
public Counter(Storage target){
storage = target;
t2 = new Thread(this);
t2.start();
}
public void run(){
try{
while(true){
storage.setValue(i);
++i;
t2.sleep(1000); // 1 second of sleep
}
}catch(Exception e){System.err.println(e);}
}
}
All this code is doing is printing out the values
that have been previously set in the Storage class.
This class is similar to the Counter class.
Code:
class Printer implements Runnable{
Thread t1;
Storage store;
public Printer(Storage target){
store = target;
t1 = new Thread(this);
t1.start();
}
public void run(){
try{
while(true){
System.out.println("The value of storage is" + store.getValue());
t1.sleep(1000); // 1 second of sleep
}
}catch(Exception e){System.err.println(e);}
}
}
The Storage class is quite simplistic but it
servers to illustrate it's point quite nicely.
All this class contains is a integer variable
and two accessor methods. That's it.......
Code:
class Storage{
int value;
synchronized void setValue(int i){value = i;}
synchronized int getValue(){return value;}
}
I think what is throwing you off is the use of
aggregation rather than inheritance.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|