dt27
asked on
Using concurrency in new JDK 1.5
Hey there,
I have this small problem, that's been bugging me for the last two hours. I'm not a Java programmer, so this might be simple. I know that in the latest JDK and JRE there is a posibility to program using concurrency. After downloading J2SE 5.0 JRE and JDK I can't run this code below:
BTW, the code is 100% working, 'cause I saw it running at another computer with J2SE 5.0
Any help greatly appreciated: code below
-------------------------- ---------- ---------
// Dekoratyvinis sodas: 3 vartai, pro kiekvienus ileidziama po 100 zmoniu.
// Koks bendras lankytoju skaicius sode?
import java.io.*;
import java.util.*;
import java.util.concurrent.Semap hore;
class Gija extends Thread {
private String vardas;
public Gija(String vardas) {
this.vardas = vardas;
}
private void laukti(int kiek) {
double x;
for (int r=0;r<kiek*100+100000*(int )Math.rand om();r++)
x=kiek*Math.random()/(Math .random()* Math.rando m()+2345.4 3);
}
private void inc() {
try {
int k;
// KS pradzia
DekSodas.sem.acquire();
k = DekSodas.bendrasLankytojuS k;
k++;
if (DekSodas.kiek>0)
laukti(DekSodas.kiek);
DekSodas.bendrasLankytojuS k = k;
DekSodas.sem.release();
// KS pabaiga
} catch (InterruptedException e) {}
}
public void run() {
for (int i=0;i<100;i++) {
inc();
}
System.out.println(vardas + " gija baige darba");
}
}
//------------------------ ---------- ---------- ---------- --
class DekSodas {
public static int kiek = 0; // giju laukimui, g.b. imama is komandines eilutes
public static int bendrasLankytojuSk = 0;
public final static Semaphore sem = new Semaphore(1, true);
public static void vykdytiGijas() {
Gija g1 = new Gija("Pirma");
Gija g2 = new Gija("Antra");
Gija g3 = new Gija("Trecia");
g1.start(); g2.start(); g3.start();
// while (g1.isAlive()||g2.isAlive( )||g3.isAl ive()) ;
try {
g3.join(); g2.join(); g1.join();
} catch (InterruptedException e) {}
}
public static void main(String[] args) throws IOException {
if (args.length > 0)
kiek = Integer.parseInt(args[0]);
vykdytiGijas();
System.out.println("bendra s lankytoju skaicius =" + bendrasLankytojuSk);
System.out.println(" Pabaiga");
}
}
I have this small problem, that's been bugging me for the last two hours. I'm not a Java programmer, so this might be simple. I know that in the latest JDK and JRE there is a posibility to program using concurrency. After downloading J2SE 5.0 JRE and JDK I can't run this code below:
BTW, the code is 100% working, 'cause I saw it running at another computer with J2SE 5.0
Any help greatly appreciated: code below
--------------------------
// Dekoratyvinis sodas: 3 vartai, pro kiekvienus ileidziama po 100 zmoniu.
// Koks bendras lankytoju skaicius sode?
import java.io.*;
import java.util.*;
import java.util.concurrent.Semap
class Gija extends Thread {
private String vardas;
public Gija(String vardas) {
this.vardas = vardas;
}
private void laukti(int kiek) {
double x;
for (int r=0;r<kiek*100+100000*(int
x=kiek*Math.random()/(Math
}
private void inc() {
try {
int k;
// KS pradzia
DekSodas.sem.acquire();
k = DekSodas.bendrasLankytojuS
k++;
if (DekSodas.kiek>0)
laukti(DekSodas.kiek);
DekSodas.bendrasLankytojuS
DekSodas.sem.release();
// KS pabaiga
} catch (InterruptedException e) {}
}
public void run() {
for (int i=0;i<100;i++) {
inc();
}
System.out.println(vardas + " gija baige darba");
}
}
//------------------------
class DekSodas {
public static int kiek = 0; // giju laukimui, g.b. imama is komandines eilutes
public static int bendrasLankytojuSk = 0;
public final static Semaphore sem = new Semaphore(1, true);
public static void vykdytiGijas() {
Gija g1 = new Gija("Pirma");
Gija g2 = new Gija("Antra");
Gija g3 = new Gija("Trecia");
g1.start(); g2.start(); g3.start();
// while (g1.isAlive()||g2.isAlive(
try {
g3.join(); g2.join(); g1.join();
} catch (InterruptedException e) {}
}
public static void main(String[] args) throws IOException {
if (args.length > 0)
kiek = Integer.parseInt(args[0]);
vykdytiGijas();
System.out.println("bendra
System.out.println(" Pabaiga");
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
:-)
ASKER