Solved

Using concurrency in new JDK 1.5

Posted on 2004-09-29
4
180 Views
Last Modified: 2013-11-23
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.Semaphore;
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.random();r++)
      x=kiek*Math.random()/(Math.random()*Math.random()+2345.43);
  }
  private void inc()  {
        try {
    int k;
      // KS pradzia
    DekSodas.sem.acquire();                  
    k = DekSodas.bendrasLankytojuSk;
    k++;
    if (DekSodas.kiek>0)
      laukti(DekSodas.kiek);
    DekSodas.bendrasLankytojuSk = 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.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("bendras lankytoju skaicius =" + bendrasLankytojuSk);
            System.out.println(" Pabaiga");
    }
}


0
Comment
Question by:dt27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 480 total points
ID: 12183140
Yes, although you ought to alter it:

a. Make DekSodas public
b. Save the code file as DekSodas.java
c. Compile and run.

This is the output:


Pirma gija baige darba
Antra gija baige darba
Trecia gija baige darba
bendras lankytoju skaicius =300
 Pabaiga
0
 
LVL 92

Assisted Solution

by:objects
objects earned 20 total points
ID: 12184886
What problems are you having running the code?
0
 

Author Comment

by:dt27
ID: 12201723
That was a very strange problem, I might say. But I got it sorted out, and concurency now works fine. Thanks for your help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12203323
:-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question