I have an application that uses multiple threads to access logic. I am using joda time to compare dates because joda time is thread safe. Below is the example method I am using. My question is being that joda time is thread safe, does it matter whether or not I am using a static method rather than an instance method?
public Map<Integer , String> Timeinterval(int value) { int i = 1440/value ; Map<Integer, String> timeInt = new HashMap<Integer, String>() ; DateTimeFormatter formatter = null ; for(int a=0 ; a< i ; a++) {
[Code] .....
The argument value gets it value from jsp at rumtime
The moment the control reaches at :- formatter = DateTimeFormat.forPattern("HH:mm");
it could not go beyond this . I executed this part
DateTimeFormat.forPattern("HH:mm"); by pressing cntrl+shift+I
and got this message :- could not resolve type: org.joda.time.format.DateTimeFormat
Basically what I want to do is increment time for eg: if time is 09:00 and I want to increment it by 30 then it should become 09:30 and this goes on till the time loop condition is satisfied, when i researched on net, I found out that Joda library is easirer to work with for such tasks.
I have been practicing writing java code, my university course is going to cover socket programming and multi-threading.
I am presently just starting to write myself a framework for all multiplayer games I may make in the future, my aim really is simply to practice and understand better and this time, im not using an ide, just sublime text, all new grounds for me, I have a good basic understanding of the subject but I want to be fluent.
import java.net.*; import java.util.ArrayList; import java.io.*; /*SMOOTH THREAD SAFE MULTI CLIENT HANDLING CLASS. * *This class will create connection objects when a connection is detected, these connections will run in a separate thread and update an array list in their parent class containing their last sent data
I have a problem with multi threading calling with for loop. I have those two and many other classes in one jar file but n 2 different packages.
When i execute the jar file first runs the Aclass, from where i try to run multi threads. From the run() of the class RunTheGame i call many other classes which are in the same package. Like you can see from the for loop is executed the threads.
The problem is that when the second thread starts the first one is stopped this happens for all the threads, more simply when a new thread starts the old one is dead.
It seems that liken the threads uses the classes called from run() of the class RunTheGame as non multi threaded. It gives this error:
Java Code:
Exception in thread "Thread-0" java.lang.NullPointerException at thePackage2.EClass.getWhiteRemaining(EClass.java:49) at thePackage2.EClass.isFinal(DClass.java:84) at thePackage2.Spiel.<init>(Cclass.java:76) at thePackage2.RunTheGame.run(RunTheGame.java:198) at java.lang.Thread.run(Thread.java:745) mh_sh_highlight_all('java');
All the above refereed classes are called from the from run() of the class RunTheGame . Like i understand all the threads use those classes as non thread autonomous classes.
Java Code:
public class Aclass { private static Thread t[]; public static void main(String[] args) throws IOException { for (int Game = 0; Game< 10; Game++) { t[Game] = new Thread(new thePackage2.RunTheGame(aplayer, bplayer, Name, ID , Pmach)); t[Game].start();
Just some questions regarding a Consumer producer program I am not understanding. Here are the two classes Below and my questions are :
1. In the main method for NamedConsumer, we created an instance of the class and call it "consumer" and we start it. Then we create another instance and we give its reference to the same variable. Doesn't this in turn destroy the original reference of the first instance we created which was started? Could we have done
NamedConsumer consumer1 = new NamedConsumer( "One", producer ); new Thread( consumer1 ).start(); NamedConsumer consumer2 = new NamedConsumer( "Two", producer ); new Thread( consumer2 ).start();
2. we called start on two difference instances of the NameConsumer class. Doesn't the "run" method need to be synchronized since two threads are hitting it?
Producer.Java
Java Code: public class Producer implements Runnable { static final int MAXQUEUE = 5; private List<String> messages = new ArrayList<String>();
public void run() { while ( true ) { putMessage(); try { Thread.sleep( 1000 );
I have been trying for days and nights , no compile error , but alot of NPEs error .......
This is my Test program
import java.util.*; public class Test { public static void main (String[]args) { int totalShares = 0 ; double totalCost = 0.0; double totalProfitLoss = 0.0;
[Code] ....
I am expecting a output like this (Which I have error running:
Tracking of Stock : FCS 8 shares has been brought at $37.61 Total shares now 8 at total cost $300.88 33 shares has been brought at $36.31 Total shares now 41 at total cost $1499.11 17 shares has been sold at $42.67 Total shares now 24 at total cost $773.72 19 shares has been sold at $32.31 Total shares now 5 at total cost $159.83 31 shares has been brought at $33.85 Total shares now 36 at total cost $1209.18 28 shares has been brought at $36.37 Total shares now 64 at total cost $2227.54 20 shares has been brought at $35.49 Total shares now 84 at total cost $2937.34 At $36.00 per share, profit is $86.66
Firstly, my code is just a demo of my multiplayer game (2 or more players can play simultaneously) to demonstrate my problem without any extra things. I have successfully implemented peer-to-peer (P2P) communication in my game. Later, I decided to add support for client-server communication (ie a central server which is also a player) in my game. It should be much easier than P2P. Now here is the problem:
Suppose, I have 1 server and some clients (may be 1 or more clients). They all should give the following output:
Starting... A B C E F ... ... Done!
They all give the above output without using multi-thread. But using multi-threading, it gives the above output only when there're 1 server and 1 client. I'm using 2 threads (1 for sending, 1 for receiving) for each Socket. That means, each client has only 2 threads for communication but the server has (2 * totalClients) threads.
I've attached my full test project. So, here I'm only showing my 2 thread classes.
ReceiveThread class: class ReceiveThread extends Thread { private ObjectInputStream receiveStream; private BlockingQueue<Character> queue = new ArrayBlockingQueue<Character>(Common.totalClients);
[Code] ....
Since I've attached my full test project, I'm not showing the other 3 classes. They are ServerMain, ClientMain and Common. If I've 2 clients to be connected, then I get the following output:
Server: (Runs first)
Starting... A Client 1 (clientID is 1): (Runs after the server)
Starting... A B B Client 2 (clientID is 2): (Runs after Client 1)
Starting... A
They are stuck at there, even no exception. Actually, the server and the clients are stuck at the line try { ch = queue.take(); } if more than 1 client are connected. It seems that all are trying to receive data. But without using the dedicated threads, they all work as expected even for more than 1 client. Why is this behaviour? How to solve it? Note that I have used the same SendThread and ReceiveThread classes by which I have succcessfully implemented P2P communication.
About my attached test project file:
It has 5 small .java files for the classes as stated above. It is currently faulty when using additional threads. You have to change clientID variable for each client (they are described inside). But it works as expected without additional threads. To test it without the additional threads:
Comment " TODO" linesUncomment the single lines just after " TODO" linesComment the additional thread construction lines (4 lines)
Currently, for a workaround, I'm not using the dedicated threads for sending and receiving data only for client-server communication in my multi-player game. But I'm still using these threads for P2P communication. I don't know why it is and how to solve it.This is the attached test project file as described above.
I am working on an assignment that I can't seem to figure out the final part to. The program takes in course data such as the time the class starts and how long it lasts. The time is in military time (0000 - 2400)
I need the output time to be the time the class started, plus the length of the class, and displayed in military time.
I can't for the life of me figure out how to do this. I have gotten a program that works for this time and minutes, and displays the correct 1020. But when I change the information to say
Start time: 0700 Length = 90 minutes
I get:
Endtime = 90
90 is technically correct, the way the formula is setup, but I need it to display 0900 not 90.
Here is the code that I have. Be easy, I'm still learning, and this is just the file I created to get the formula to work. Also, the verbose in here is just for my own debugging to make sure values should be what I'm expecting them to be.
public class calc { public static void main(String[] args) { double hours, minutes, length; double temp; int time = 2400; hours = time / 100; System.out.println("Hours are: " + hours);
I have two classes. time_runner is used for testing my code.
This is what I'm using to test my code:
class time_runner { public static void main(String str[]) throws IOException { Time time1 = new Time(14, 56); System.out.println("time1: " + time1); System.out.println("convert time1 to standard time: " + time1.convert()); System.out.println("time1: " + time1); System.out.print("increment time1 five times: "); time1.increment();
[code]....
The two constructors are "Time()", which is the default constructor that sets the time to 1200, and "Time(int h, int m)" Which says If h is between 1 and 23 inclusive, set the hour to h. Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, set the minutes to m. Otherwise, set the minutes to 0. Those are my two constructors that I pretty much have down. The three methods however I'm having trouble with. The "String toString()" Returns the time as a String of length 4. The "String convert()" Returns the time as a String converted from military time to standard time. The "void increment()" Advances the time by one minute.
public class Time { private int hour; private int minute; public Time(int h, int m) { if(h > 1 && h < 23) hour = h;
Below is the main class of a project ive been working on, the goal is to start a countdown specified by the user. When the countdown reaches zero the base drops in the song that is being played. (Its not done yet) The main problem that arises is the fact that my song plays, and AFTER that, the timer starts.
Output:
Please input countdown in HH:mm:ss format. 00:00:41 Start? Yes
The name of of the song is: Skrillex & Damian "Jr Gong" Marley - "Make It Bun Dem"
The time of base drop is: 00:00:41 //Song starts here
//Song is done //Then timer starts 00:00:41 00:00:40 00:00:39
I would like to set a specific time for a Calendar instance. My below code will set a time one minute ahead of the current time, and format it to ISO8601 standard.
I now want to set another variable(deadlineDate), but to a specific time, say 5 minutes ahead. I would like to hardcode this in so I dont want it to change as a result of getting the instance of the Calendar(which sets it to the current time)
Essentially I am trying to regenerate the above code every minute and increment itself each cycle until it reaches the deadlineDate, which is a fixed datetime.
I am writing a code that tries to figure out the users password by going through every possible key (brute force). Although I think its going to work, it looks EXTREMELY inefficient to me, since its just a huge switch statement for each character -- 94 total, including the shift values. Is there a built in method in the JAVA API that goes through every key or something?
Here is my code for that segment:
public char[] HackingPassword(){ char[] passChars = this.password.toCharArray();//convert the password to char[] char[] hacking = new char[passChars.length];//make the hacking variable same size as users password int nextCharValue = 0;//this is used to cycle through the keyboard //check each letter of hacking to match with password for(int i = 0; i < passChars.length; i++){
Yes my JPanel is not displaying all the time like you would think it would. Before you beat me up yes I am a bit new I am just working on the GUI part of java over my summer break. Obviously it is not suppose to do that. Secondly, As you can see i am using the alphabet to create JButtons. Shouldnt there be an easier way to use a for( loop) . I tried before but I didn't want to get too side tracked.
Java Code:
import javax.swing.*; import java.awt.*; public class hello { private JFrame alphaframe1; private JFrame alphaframe2;
I'm having an issue with this little bit of conversion. I'm converting a string (_dateString) into a Calendar time. I am using DateFormat and SimpleDateFormat to accomplish this task. Everything seems to be working great, except for it figuring out whether it is AM or PM. According to SimpleDateFormat (Java Platform SE 7 ) I am using "aa" to get my AM or PM marker. How come in my output then, it believes it to be 4:45 am instead of 4:45 pm? Hour of Day should return the 24 hour clock, which should show 16 instead of 4. I have posted the output below my code.
Java Code:
import java.text.*; import java.util.*; public class Time{ static String _dateString = "08 Feb 2014, 4:45pm"; public static void main(String args[]){ Calendar cal=Calendar.getInstance();
[Code] ....
Output:
Java Code:
Today is Sat Feb 08 04:45:00 EST 2014 Year: 2014 Month: 1 Day of Month: 8 Day of Week: 7 Week Of Year: 6 Week of Month: 2 Hour: 4 Hour of Day: 4 Minute: 45 Second: 0 Millisecond: 0 mh_sh_highlight_all('java');
I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.
//Create Scanner object Scanner keys = new Scanner(System.in); //Get chips System.out.print("How much money would you like to change? "); int chips = keys.nextInt(); System.out.println("You now have $" + chips + " in chips.");
[code]...
This is what I get when I run it run/How much money would you like to change?
I work as a golf staff I would like to create application which would tell me the playing time and the time when players need to reach certain playing field/hole. my ideas is to make a program which would ask the user to input their starting time and than select the hole number, where the end result would be amount of actuall time.
I have been having hard time to figure out howe to properly structure the input conversion so it is recognized as a time (Exampke: 10:15). Do I need to use the calendar method in Java or ?
I am trying to to insert time onto my jFrame (Netbeans) by using textfields. this is the code i've tried to put into the textfield so whenever i run the program it automatically shows the time.
I Have one query regarding OTP, In my project while registration the otp should be generate and send to the mobile number of user which is registered the form.
I have this code that prints the run time for each for loop iteration but how do I find the fastest time store it in a variable and the print that variable at the end? This is what I have currently.
for (int i = 1; i <= 14; i++) { int n = (int) Math.pow(2, i); int[] list = new int[n]; for (int j = 0; j < list.length; j++) { list[j] = (int) (Math.random() * 2000); } int length = list.length; double radixTime = radixSort(list, length); System.out.println("For base " + i + " the time was " + radixTime); }