What Is Main Use Of Synchronization In Java
Feb 19, 2015I have no clear idea about Synchronization..What is main purpose of it and where it is useful in realtime.
View RepliesI have no clear idea about Synchronization..What is main purpose of it and where it is useful in realtime.
View RepliesI have a class (WriteExcel) which writes to an excel file. For those who don't know, Excel doesn't handle multiple access well (or at all).
I then have another class (WriteManager) which creates a new thread for each write command, and then calls a method from the WriteExcel class.
Other classes in my project calls methods from WriteManager to send their requests. My situation looks like this:
1) Thread 1: Someclass -> WriteManager.write1stPage(write) -> Creates Thread 2 -> WriteExcel.write1stPage(write) -> Start Excel Write
2) Thread 1: Someclass -> WriteManager.write2ndPage(write) -> Creates Thread 3 -> WriteExcel.write2ndPage(write) -> Start Excel Write
3) Thread 1: Someclass -> WriteManager.write3rdPage(write) -> Creates Thread 4 -> WriteExcel.write3rdPage(write) -> Start Excel Write
4) Thread 2: Finished writing and ready to save -> Throws exception due to multiple Excel access
5) Thread 3: Finished writing and ready to save -> Throws exception due to multiple Excel access
6) Thread 4: Finished writing and ready to save -> Throws exception due to multiple Excel access
I need to figure out a way to restrict access to WriteExcel until the writing is finished. So I need something like this:
1) Thread 1: Someclass -> WriteManager.write1stPage(write) -> Creates Thread 2 -> WriteExcel.write1stPage(write) -> Start Excel Write
2) Thread 1: Someclass -> WriteManager.write2ndPage(write) -> Creates Thread 3 -> Waits Until WriteExcel is free
3) Thread 1: Someclass -> WriteManager.write3rdPage(write) -> Creates Thread 4 -> Waits Until WriteExcel is free
4) Thread 2: Finished writing and ready to save -> Saves and frees WriteExcel
5) Thread 3: WriteExcel is free -> WriteExcel.write2ndPage(write) -> Start Excel Write
6) Thread 3: Finished writing and ready to save -> Saves and frees WriteExcel
7) Thread 4: WriteExcel is free -> WriteExcel.write3rdPage(write) -> Start Excel Write
8) Thread 4: Finished writing and ready to save -> Saves and frees WriteExcel
What is currently the best approach in java for handling this sort of situation?
There is method in the class of my application and there are four other class which is running in different machine means different jvm and these classes are accessing the method of my class. then how to protect my method from accessing other class at the same time. I am not able to use synchronized keyword because this works only for single jvm.
View Replies View RelatedIs it possible to run a program(a method) without a main method?
View Replies View RelatedI am trying to make a 2d graphical animation using the java swing classes in order to make a frame. I had a basic version working when all of the code was under the main method, but when I moved some into another method it broke it. With y understanding of java my code should work as I create a variable of the method containing the code and then assign the size and exit button. However this causes many problems such as my BeaconFrame method informing me that it is unused when I have used it. Here is my code:
import javax.swing.*;
import java.awt.*;
public class BelishaBeacon {
public void BeaconFrame() {
JFrame frame = new JFrame();
JPanel panel1 = new JPanel();
[code]....
I am making an application in java, inside static void main, i want to customize all buttons, text areas and want to put them on desired location inside application. I have tried to use setbounds but can not use it, how can i use it, or is there any other way or layout to make my application components customized layout.
View Replies View RelatedI have style sheet file (e.g. myStyle.xslt). I am trying to decide where to put this file in my web application so that both src and test can access it. All my source in packages inside /src folder and test code in packages inside /test folder.
View Replies View Relatedi have a run time error that could not find or load the main file java .
View Replies View RelatedWhen I try to run Java from the Windows DOS command line, I'm running into trouble:
1. When I run Java from the wrong directory, I get the error Error: cannot find or load main class myapp1.
2. When I get to the "right" directory (.../MyApp1/build/classes/myapp1/, which is where the MyApp1.class file resides), I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: MyApp1 (wrong name: myapp1/MyApp1)
1. The CLASSPATH variable is not set (when I type echo %CLASSPATH% the system returns %CLASSPATH%).
2. I also get the same error above when I use: > java -cp . myapp1
3. I've made sure the PATH is set correctly in environment variables, with the Java path at the beginning of the path string).
I currently use Eclipse to maintain our Java application. I recently upgraded from Java 6 to Java 7. I updated my Eclipse projects to use the Java 7 .jar files. I can run the application from Eclipse via the Run Configuration.
I can also run the Ant build and it completes successfully. When I install the application on my desktop, I receive the "Java Virtual Machine Launcher: Could not find main class..." error. My CLASSPATH is set to ".".
what is different between void main() and public void main()
View Replies View RelatedI'm trying to make a TBG (Text Based Game) in Java for Android. Now, when I try to run it, it says No Main Method to run this program. The compiler also checks the program and tell me there are no errors.
View Replies View RelatedWhy we do not need main in applet .....
View Replies View Relatedimport java.io.*;
import java.util.List;
import java.util.ArrayList;
public class ListOfNumbers {
try
[code]....
i tried this code in BlueJ IDE.it told me i had an illegal beginning for "try".am i not supposed to use try for main methods?
What happened when we use main() as a private. . . . .???????
View Replies View RelatedI've made a class called Car with a method which will tell me a category for the engine size regarding the actual size (which I've included in the main just so I could see if it works) but everytime I test it I get an error.
public class Car {
public String b;
public String c;
public double es;
public double cs;
public String getCategory() {
if (es < 1.3)
[Code] ....
Figured it out. Was missing parenthesis on audiCar.getCategory();
in java why main method is always static in nature...
View Replies View RelatedI am writing a code where in the first method the question will ask whats your favorite website. for example www.javaprogrammingforums.com...when it outputs it will read just "javaprogrammingforums" without the www. and the .com.
Because the program will ask a series of questions in the main, I would like website question to be returned to the main. Here is my code, and what can I do?
import java.util.Scanner;
public class chapter3 {
public static String website(Scanner kb) {
String website;
System.out.println("What is your favorite website?");
website = kb.next();
[Code] ......
How do I call a class within the main. For example, this is what i have in my code right now. I am trying to call the class BlackJackGame.
package blackjack;
public class BlackJack {
public BlackJack() {
BlackJackGame();
}
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int hours = getHours(kb);
char major = getMajor(kb);
[code]....
I'm trying to return a char c,o, or x if that is their "major code" that the scanner grabbed.
I have 3 source file below
//Hello.java
public interface Hello {
public void sayHello();
}
//Espanol.java
public class Espanol implements Hello {
[Code] ....
The idea is to create a program to add plants and retrieve plants from a Nursery. I created a class with the properties of the plants and also there is the class an Array list to keep track of the plants entered ( they will have to be printed later) (I am not too sure if adding a public class there is the best option.
The program will allow the user to pick and action and from there the code will do something. I don't want to have all the code inside 'main' The problem is in line 114.This is what I have so far.
Java Code:
package plant.nursery;
import java.util.ArrayList;
import java.util.Scanner;
/**Class to create a plant for a nursery.
public class PlantNursery
[code]....
this is my program error occured run time,but compile sucessfully
{error msg-Error: Main method not found in class helloworldapp.HelloWorldApp, please define the main method as:
public static void main(String[] args)}
class HelloWorldApp
{
private int empno;
private float salary;
void setHelloWorldApp()
{
empno=12;
salary=1200;
[code]....
How do I call a class within the main. For example, this is what i have in my code right now. I am trying to call the class BlackJackGame.
package blackjack
public class BlackJack {
public BlackJack() {
BlackJackGame();
}
}
For some reason when I try to run the program it gives me an "editor does not contain a main type" launch error.
/*
* A program that plays and scores a round of the game Poker Dice. In this game, five dice are rolled. The player is allowed to select a number of those five dice to re-roll. The dice are re-rolled and then scored as if they were a poker hand. The following hands MUST be scored in this assignment:
* * High card
* * One Pair
* * Two Pair
* * Three of a Kind
* * Full House
* * Four of a Kind
* * Five of a Kind
* For extra credit, you may also implement:
* * Straight
*/
import java.util.Scanner;
public class Project10 {
public static void main(String[] args) {
Scanner inScanner = new Scanner(System.in);
int[] dice = new int[5];
resetDice(dice);
System.out.print("Your current dice: + dice");
[code]....
I keep getting a zero dollar return when I run my main. I am multiplying grossPay by hours but something is not catching.
public class employee {
private String name; //employees name
private int id; //employees id number
[Code].....