need example of multi-threaded java pgm - enables all threads to get items

J

johndesp

Hi.


I want to startup 3 threads which will access elements of a Vector and
perform an action based upon the value in the vector. I want to each
thread to get priority to the Vector to grab the next element in order
that the particular thread completed enacting on the previous Vector
element.


Forinstance, Assuming each thread gets started in order..


The first three elements of the vector would get executed in order


Thread 0 ==> element 0
Thread 1 ==> element 1
Thread 2 ==> element 2


However if Thread 1 finished before the others it would grab the next
vector element..


Thread 1 ==> element 3
Thread 2 ==> element 4
Thread 0 ==> element 5


I can get the threads started but thread 0 always seems to have a
"lock" on the Vector and the other threads can't get involved..


I would appreciate it if someone could provide a solution and perhaps
some psuedo code to make this happen. Like I said before I can get all
3 threads to start , but thread 0 hogs up the Vector...


thanks
 
J

johndesp

Class which starts the threads....

import java.io.*;

public class Vex {

public static void main(String[] args) {

VecRecs vr = new VecRecs();
//for (int c = 0; c < vr.getSize(); c++) {
//System.out.println(vr.getRec());

//}

dezy jaclyn[] = new dezy[3];
try {
for (int i = 0; i < 3; i++) {

jaclyn = new dezy(vr, i);
jaclyn.start();
}
System.out.println("Started threads");

for (int i = 0; i < 3; i++) {

jaclyn.join();
System.out.println("Thread " + i + " is done.");
}

} catch(Exception e) {
e.printStackTrace();
}

}
}

================= class which extends Thread
===============================


import java.util.*;
import java.io.*;

public class dezy extends Thread{
VecRecs rex ;
Hashtable ht;
String thingy = "";
int threadnum = 4;

public static void main(String[] args) {
}
public dezy (VecRecs inrex,int inthreadnum) {
rex = inrex;
threadnum = inthreadnum;
}

public void run(){


try{
thingy = (String)rex.getRec();
while (thingy != null){
thingy = (String)rex.getRec();
System.out.println("Thread Number is " + threadnum + " and " +
thingy);

}

}
catch(Exception e) {
e.printStackTrace();
}


}
}

======================= Class which holds the vector
======================


import java.util.*;
public class VecRecs {
Vector newV = new Vector(5);
int holder = -1;
int holdernew = 0;
Object returner = null;
LinkedList ht;
public static void main(String[] args) {
}

public VecRecs() {
System.out.println("in constructor");
newV.addElement("first");
newV.addElement("second");
newV.addElement("third");
newV.addElement("fourth");
newV.addElement("fith");

}
public synchronized Object getRec(){
try {


holder = holder + 1;


if(holder < newV.size()){

returner = newV.elementAt(holder);}
else {
returner = null;
}

}catch (Exception e){

}
this.notify();
return returner;

}
public int getSize() {

return newV.size();
}
}


====== Current output =====
notice threads 1,2 aren't even used.
in constructor
Thread Number is 0 and second
Thread Number is 0 and third
Thread Number is 0 and fourth
Thread Number is 0 and fith
Thread Number is 0 and null
Started threads
Thread 0 is done.
Thread 1 is done.
Thread 2 is done.
 
J

johndesp

Class which starts the threads....

import java.io.*;

public class Vex {

public static void main(String[] args) {

VecRecs vr = new VecRecs();
//for (int c = 0; c < vr.getSize(); c++) {
//System.out.println(vr.getRec());

//}

dezy jaclyn[] = new dezy[3];
try {
for (int i = 0; i < 3; i++) {

jaclyn = new dezy(vr, i);
jaclyn.start();
}
System.out.println("Started threads");

for (int i = 0; i < 3; i++) {

jaclyn.join();
System.out.println("Thread " + i + " is done.");
}

} catch(Exception e) {
e.printStackTrace();
}

}
}

================= class which extends Thread
===============================


import java.util.*;
import java.io.*;

public class dezy extends Thread{
VecRecs rex ;
Hashtable ht;
String thingy = "";
int threadnum = 4;

public static void main(String[] args) {
}
public dezy (VecRecs inrex,int inthreadnum) {
rex = inrex;
threadnum = inthreadnum;
}

public void run(){


try{
thingy = (String)rex.getRec();
while (thingy != null){
thingy = (String)rex.getRec();
System.out.println("Thread Number is " + threadnum + " and " +
thingy);

}

}
catch(Exception e) {
e.printStackTrace();
}


}
}

======================= Class which holds the vector
======================


import java.util.*;
public class VecRecs {
Vector newV = new Vector(5);
int holder = -1;
int holdernew = 0;
Object returner = null;
LinkedList ht;
public static void main(String[] args) {
}

public VecRecs() {
System.out.println("in constructor");
newV.addElement("first");
newV.addElement("second");
newV.addElement("third");
newV.addElement("fourth");
newV.addElement("fith");

}
public synchronized Object getRec(){
try {


holder = holder + 1;


if(holder < newV.size()){

returner = newV.elementAt(holder);}
else {
returner = null;
}

}catch (Exception e){

}
this.notify();
return returner;

}
public int getSize() {

return newV.size();
}
}


====== Current output =====
notice threads 1,2 aren't even used.
in constructor
Thread Number is 0 and second
Thread Number is 0 and third
Thread Number is 0 and fourth
Thread Number is 0 and fith
Thread Number is 0 and null
Started threads
Thread 0 is done.
Thread 1 is done.
Thread 2 is done.
 
H

Harish

from what i can see, your code looks ok.
can you put a thread.yield()/sleep(0) inside the run method?
if that doesn;t work put a bigger sleep, like 100 or soemthing.
 
J

johndesp

Harish said:
from what i can see, your code looks ok.
can you put a thread.yield()/sleep(0) inside the run method?
if that doesn;t work put a bigger sleep, like 100 or soemthing.


Class which starts the threads....

import java.io.*;

public class Vex {

public static void main(String[] args) {

VecRecs vr = new VecRecs();
//for (int c = 0; c < vr.getSize(); c++) {
//System.out.println(vr.getRec());

//}

dezy jaclyn[] = new dezy[3];
try {
for (int i = 0; i < 3; i++) {

jaclyn = new dezy(vr, i);
jaclyn.start();
}
System.out.println("Started threads");

for (int i = 0; i < 3; i++) {

jaclyn.join();
System.out.println("Thread " + i + " is done.");
}

} catch(Exception e) {
e.printStackTrace();
}

}
}

================= class which extends Thread
===============================


import java.util.*;
import java.io.*;

public class dezy extends Thread{
VecRecs rex ;
Hashtable ht;
String thingy = "";
int threadnum = 4;

public static void main(String[] args) {
}
public dezy (VecRecs inrex,int inthreadnum) {
rex = inrex;
threadnum = inthreadnum;
}

public void run(){


try{
thingy = (String)rex.getRec();
while (thingy != null){
thingy = (String)rex.getRec();
System.out.println("Thread Number is " + threadnum + " and " +
thingy);

}

}
catch(Exception e) {
e.printStackTrace();
}


}
}

======================= Class which holds the vector
======================


import java.util.*;
public class VecRecs {
Vector newV = new Vector(5);
int holder = -1;
int holdernew = 0;
Object returner = null;
LinkedList ht;
public static void main(String[] args) {
}

public VecRecs() {
System.out.println("in constructor");
newV.addElement("first");
newV.addElement("second");
newV.addElement("third");
newV.addElement("fourth");
newV.addElement("fith");

}
public synchronized Object getRec(){
try {


holder = holder + 1;


if(holder < newV.size()){

returner = newV.elementAt(holder);}
else {
returner = null;
}

}catch (Exception e){

}
this.notify();
return returner;

}
public int getSize() {

return newV.size();
}
}


====== Current output =====
notice threads 1,2 aren't even used.
in constructor
Thread Number is 0 and second
Thread Number is 0 and third
Thread Number is 0 and fourth
Thread Number is 0 and fith
Thread Number is 0 and null
Started threads
Thread 0 is done.
Thread 1 is done.
Thread 2 is done.


Harish, thanks. I would really like to accomplish this without a
sleep. This program captures timings and I need them to be as accurate
as possible. Thanks for the help
 
H

Harish

Harish, thanks. I would really like to accomplish this without a
sleep. This program captures timings and I need them to be as accurate
as possible. Thanks for the help
i was just making sure that with yield/sleep you see other threads are
getting a chance to
do getRec. if it does, then it tells you that your code is working as
designed.
its the operating system which schedules your thread for execution, and in
this particular
case the OS executes the same thread again and again. why does it do that? i
dont know it all depends on the scheduling algorithm that they use.
it may be because the run is small and it does not major IO processing(even
though i would consider the println as IO)
so OS keeps executing the same thread. this is ok, this means the CPU is
busy, it not idling, its not waiting on some IO to complete
so switching to some other thread during this time is inefficient since
thread context switching is expensive.

you will see other threads getting executed if there were more items in the
vector(like 100s or even more), but there is no guarantee...
or if you have multiple CPU. if your run method is CPU intensive,(no file
writes, network reads etc) then in a single CPU
machine you don't gain anything by making it multi-threaded. a sequential
processing will be more efficient since
you don't have the overhead of thread context switching.
 
A

Andrew McDonagh

Harish said:
from what i can see, your code looks ok.

Are you looking at the same code below, like me?

Why are the 3 threads being joined?

Why are there unused member vars.

Why are member vars used when they could be local to the method.

The list goes on....
can you put a thread.yield()/sleep(0) inside the run method?
if that doesn;t work put a bigger sleep, like 100 or soemthing.


Class which starts the threads....

import java.io.*;

public class Vex {

public static void main(String[] args) {

VecRecs vr = new VecRecs();
//for (int c = 0; c < vr.getSize(); c++) {
//System.out.println(vr.getRec());

//}

dezy jaclyn[] = new dezy[3];
try {
for (int i = 0; i < 3; i++) {

jaclyn = new dezy(vr, i);
jaclyn.start();
}
System.out.println("Started threads");

for (int i = 0; i < 3; i++) {

jaclyn.join();
System.out.println("Thread " + i + " is done.");
}

} catch(Exception e) {
e.printStackTrace();
}

}
}

================= class which extends Thread
===============================


import java.util.*;
import java.io.*;

public class dezy extends Thread{
VecRecs rex ;
Hashtable ht;
String thingy = "";
int threadnum = 4;

public static void main(String[] args) {
}
public dezy (VecRecs inrex,int inthreadnum) {
rex = inrex;
threadnum = inthreadnum;
}

public void run(){


try{
thingy = (String)rex.getRec();
while (thingy != null){
thingy = (String)rex.getRec();
System.out.println("Thread Number is " + threadnum + " and " +
thingy);

}

}
catch(Exception e) {
e.printStackTrace();
}


}
}

======================= Class which holds the vector
======================


import java.util.*;
public class VecRecs {
Vector newV = new Vector(5);
int holder = -1;
int holdernew = 0;
Object returner = null;
LinkedList ht;
public static void main(String[] args) {
}

public VecRecs() {
System.out.println("in constructor");
newV.addElement("first");
newV.addElement("second");
newV.addElement("third");
newV.addElement("fourth");
newV.addElement("fith");

}
public synchronized Object getRec(){
try {


holder = holder + 1;


if(holder < newV.size()){

returner = newV.elementAt(holder);}
else {
returner = null;
}

}catch (Exception e){

}
this.notify();
return returner;

}
public int getSize() {

return newV.size();
}
}


====== Current output =====
notice threads 1,2 aren't even used.
in constructor
Thread Number is 0 and second
Thread Number is 0 and third
Thread Number is 0 and fourth
Thread Number is 0 and fith
Thread Number is 0 and null
Started threads
Thread 0 is done.
Thread 1 is done.
Thread 2 is done.

 
H

Harish

i looked at the code in terms of functionality...as in, is the code really
creating multiple threads and running it...

join is waiting for threads to finish. i dont see any issue with that,
rather a good way to program.

about unused member vars and local vars: i didnt look into it.

when I said OK, I didnt mean the code is perfect...but "should create
multiple threads and execute it".

Andrew McDonagh said:
Harish said:
from what i can see, your code looks ok.

Are you looking at the same code below, like me?

Why are the 3 threads being joined?

Why are there unused member vars.

Why are member vars used when they could be local to the method.

The list goes on....
can you put a thread.yield()/sleep(0) inside the run method?
if that doesn;t work put a bigger sleep, like 100 or soemthing.


Class which starts the threads....

import java.io.*;

public class Vex {

public static void main(String[] args) {

VecRecs vr = new VecRecs();
//for (int c = 0; c < vr.getSize(); c++) {
//System.out.println(vr.getRec());

//}

dezy jaclyn[] = new dezy[3];
try {
for (int i = 0; i < 3; i++) {

jaclyn = new dezy(vr, i);
jaclyn.start();
}
System.out.println("Started threads");

for (int i = 0; i < 3; i++) {

jaclyn.join();
System.out.println("Thread " + i + " is done.");
}

} catch(Exception e) {
e.printStackTrace();
}

}
}

================= class which extends Thread
===============================


import java.util.*;
import java.io.*;

public class dezy extends Thread{
VecRecs rex ;
Hashtable ht;
String thingy = "";
int threadnum = 4;

public static void main(String[] args) {
}
public dezy (VecRecs inrex,int inthreadnum) {
rex = inrex;
threadnum = inthreadnum;
}

public void run(){


try{
thingy = (String)rex.getRec();
while (thingy != null){
thingy = (String)rex.getRec();
System.out.println("Thread Number is " + threadnum + " and " +
thingy);

}

}
catch(Exception e) {
e.printStackTrace();
}


}
}

======================= Class which holds the vector
======================


import java.util.*;
public class VecRecs {
Vector newV = new Vector(5);
int holder = -1;
int holdernew = 0;
Object returner = null;
LinkedList ht;
public static void main(String[] args) {
}

public VecRecs() {
System.out.println("in constructor");
newV.addElement("first");
newV.addElement("second");
newV.addElement("third");
newV.addElement("fourth");
newV.addElement("fith");

}
public synchronized Object getRec(){
try {


holder = holder + 1;


if(holder < newV.size()){

returner = newV.elementAt(holder);}
else {
returner = null;
}

}catch (Exception e){

}
this.notify();
return returner;

}
public int getSize() {

return newV.size();
}
}


====== Current output =====
notice threads 1,2 aren't even used.
in constructor
Thread Number is 0 and second
Thread Number is 0 and third
Thread Number is 0 and fourth
Thread Number is 0 and fith
Thread Number is 0 and null
Started threads
Thread 0 is done.
Thread 1 is done.
Thread 2 is done.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top