I have two threads, one generates data based on some decision process (A) and another use the data to plot it (B). The problem occurs when there is no data to plot. What is the best way to synchronize these two threads? Using sleep, or wait/notify?
import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; public class tree_lock_test{ int total_instances; int thread_instances = 0; int N;
[Code] .....
this is compiled with another Peterson class which has implemeted peterson lock for two threads ...
I need to generate a bar chart given the data that is in main. The relative performance of each sorting algorithm with respect to time, number of comparisons, and number of swaps for each data set (e.g., 50,000 –400,000 random integers). Each algorithm will have 5 bars corresponding to the 5 data sets. The height of these bars will depend on the performance of the algorithm for that data set.
I know how to write a bar with normal values like integers but for this I'm not sure because the values for instance comparisons and swaps are not given they are calculated. I just need to know what steps to go about doing that, you can even express them in pseudocode or even just explaining it I don't need the code, I think. Here is the main
public class Main { public static void main(String[] args){ int t= 50000; int t2=100000; int t3=200000; int t4=300000; int t5=400000;
I am currently concentrating on Java programming skills. Right now developing a Energy usecase in Java with Spring framework. Have business requirement to generate data along with outliers. For example, have two parameters called as min and max (centigrates). Generate a file between min and max and store in a file. Atleast need to generate 10 outliers in each 100 records. Outliers should be < min and > max. How to write a java class with this business condition. The file should be like,
Java Code:
Public class DataGeneration(int min, int max) where min = 30 max = 50
I have a string of variable tags (tag) filled with variables, and using a Scanner (scan) to manipulate and generate data dictionary add cards for each. The variables are delimited with new line character, and the variables are structured such that the actual name of the variable contains info as to its data type, size, etc. What I need to do is move the scanner to a particular point into the string then use .nextInt() or similar. I don't want to do this to the whole variable, just where I start it from. How can I do this?
Scanner scan = new Scanner( tag ); int ptr = tag.indexOf( "_" ); ptr++; if ( Character.toLowerCase( tag.charAt( ptr ) ) == 's' ) // then start at the "_" character and grab the nextInt()
Assignment 13 – Plot functions using abstract methods (Plot functions using abstract methods) Write an abstract class that draws the diagram for a function. The class is defined as follows:
Java Code:
public abstract class AbstractDrawFunction extends JPanel { /** Polygon to hold the points */ private Polygon p = new Polygon(); protected AbstractDrawFunction() { drawFunction();
[code].....
For each function, create a class that extends the AbstractDrawFunction class and implement the f method. Figure below displays the drawing for the first three functions.how to make the jframe to work so that I displays the panel
Assignment 13 – Plot functions using abstract methods
Write an abstract class that draws the diagram for a function. The class is defined as follows:
public abstract class AbstractDrawFunction extends JPanel { /** Polygon to hold the points */ private Polygon p = new Polygon(); protected AbstractDrawFunction() { drawFunction();
For each function, create a class that extends the AbstractDrawFunction class and implement the f method. Figure below displays the drawing for the first three functions.
Here is the outline of what the program should do: [URL] .....
So far I've got the very basics in place, I can't really figure out ptplot despite trying to research it online. The only other thing I know is that arrays are not to be used (initially I intended on using them). Here's my code:
//The next line imports the java.io package import java.io.*; //Imports math language import java.lang.Math; //import ptolemy.plot.*; //Sets up class public class cp5 { //Declares class method called 'main'.
I'm new to Java and trying to write code a Java program on Mac OS X using IntelliJ. My program uses the SWT library and contains two class's; the first called "view" and the second called "main". The "view" class defines the SWT objects, extends the "Thread" class and contains a "run" method;
public void run() { initComponents(); while (!display.isDisposed()) { if (!display.readAndDispatch()) {
display.sleep();
[code]....
I searched for a solution and saw that I have to use the "-XstartOnFirstThread" parameter to JVM. I'm trying it with no success.
I making a program that a client connect to a server, then it's starts changing information throw the socket. The info is String. When connection with one open client everything is working great. The problem starts when I connect 2 or more clients simultaneously.
The server doesn't know how to handle each request so it's send info to both the client info that is wrong. If I run several clients and then execute the last client that opens he will work fine the others will crush. On the server I'm getting connection reset. The problem i believe is with the closing socket and thread holding.
I have this web app in Glassfish which, among other things, monitors consultations in some DB. It's a JEE-EAR app, three layers. Pretty boring until now. Now, there's another WAR-app on Tomcat that processes files through threads. These threads represent an Excel file processed one row at a time.
I need to know when one of those threads are created, when they're alive and when they're terminated, from the Glassfish app.
I need to monitor these batch processes.
I think I could insert the thread ID from the tomcat app in some DB and when it dies, delete it. The glassfish app would query that BD and see if there is one of those batch processes running.
I understand that a thread ID can be recycled but I can find a way to make every process unique.
My first question, would this be viable?
My second question is, could I uniquely set the thread name and then ask for it from the glassfish app to the tomcat-app process thread set? I mean, without a DB in the middle?
Write a program to print the even numbers and the odd numbers between 0 and 30 using a single thread and then again using multiple threads.
I already finished the single-threaded program but am having trouble with the multi-threaded one. I have three classes; one class for odd numbers, one for even numbers, and one to execute the code. Here is my code so far:
Even Numbers:
Java Code:
public class EvenNumbers extends Thread { public void run() { for (int i=1; i<=30; i++) { if (i%2 == 0) { System.out.println("Even number " + i);
[Code] ....
Unfortunately, my output ends up looking a little weird:
Java Code:
C:UsersREDACTEDDropboxSchoolworkREDACTEDJava ProgrammingUnit 5 - Exception Handling, AssertionsProgram - Thread>java MultiThread Even Numbers: Odd Numbers: Even number 2
I'm reading the following section of the Oracle docs:
Guarded Blocks (The Java Tutorials > Essential Classes > Concurrency)
We have multiple threads. One of them sets the joy flag to true. The other waits until joy flag is set to true in order to print to the output stream. Rather than squander processer resources with a while loop, we choose to use the wait method of Object which suspends execution of thread. When the other thread throws an exception, we check the loop condition again.
Java Code:
public synchronized void guardedJoy() { // This guard only loops once for each special event, which may not // be the event we're waiting for. while(!joy) { try { wait(); } catch (InterruptedException e) {} } System.out.println("Joy and efficiency have been achieved!"); } mh_sh_highlight_all('java');
The documentation goes on to state the following:
When a thread invokes d.wait, it must own the intrinsic lock for d - otherwise an error is thrown. Invoking wait inside a synchronized method is a simple way to acquire the intrinsic lock. When wait is invoked, the thread releases the lock and suspends execution.
The statement seems somewhat contradictory. When we invoke wait inside a synchronized method, is the intrinsic lock acquired or released? I thought it was the synchronized keyword itself that acquired the intrinsic lock.
My multi threaded application processes and loads records into an ECM repository. For reconcliation purposes , I am trying to build an XML format report with the results of the processing per record, while processing is underway per thread. The method below is called from every thread wvery time its ready to append an element to the DOM with the status.
public void writeReportLine(Element rptLine) { // Write output to report file synchronized (XMLReportHandler.class) { reportOutput.getDocumentElement().appendChild(rptLine); } }
After all processing completes, the below method is called only once by every thread to write to the File on the file system:
public void writeToReportFile() { synchronized (XMLReportHandler.class) { try{ //write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(reportOutput);
[Code] ....
The problem is that when under load, the threads just seem to hang while the transformer.transform(source, result) call keeps getting executed until there is an interrupt of some sort. I was able to examine a section of what was appended and it was status for records that had finished processing very early in the process based on my application logs. Once an interrupt is recieved , it looks like the threads recover.
I created an instant messenger using java. When I have the Server that communicates between the clients and one client running on my Computer the CPU Usage is at 100%. It really slows down everything else I'm doing and I figure this might be an issue if I gave this to people to use. I don't want the client taking up a lot of CPU Usage if they're just running it in the background while doing other things on their computer. The program utilizes multithreading. Each thread is constantly being polled for input.
The Server, as seen below, has two threads. I explain what the threads do before the code. There is also another while loop running constantly in the server that is waiting for sockets to connect. The loop does not run constantly at the line socket.accept(); it stops and just waits.
The User, split into a menu and chat window, has two threads as well. I explain what the threads do before the code. After I originally posted I put a 100 ms sleep in all my threads. CPU Usage is still at 100%*
This thread listens for input from the user. The input tells the server what action to take. There is a thread running for every user currently connected to the server.
public void run() { try { input = new DataInputStream(user.getSocket().getInputStream()); output = new DataOutputStream(user.getSocket().getOutputStream());
I have a console application. One thread allows a user to directly input into a console. Another thread listens on a tcp port, takes input, processes it, and then writes it to the console. The work in different threads, but in tcp thread, one I call a method outside that thread that writes to console, it often gets stuck. here is a mockup of situation:
It often freezes on "Attempt 1". Before I used System.out there, I also tried console.writer() there but both freeze at that point often. Any situation where console or System.out.writeln freeze when working across threads and why it is occurring? It almost feels like one thread has locked the console so the others can't write to it.
I have been working on this program for a while and now i seem to be stump it throws an outof Bound array exception error, this program is a matrix multiplication program and spits out the resulting matrix using multithreading. i have a running one and result is
2 -1 0 1 0 3 -1 1 3
but this program's result is:
2 0 0 1 0 0 -1 0 0
it reads a txt document as an commandline arguement the text file reads just like this below:
3 2 2 3 1 0 0 1 -1 1 2 -1 0 1 0 3
the following is my code:
import java.util.*; import java.io.*; public class P3 { public static int matrix1[][]; public static int matrix2[][];
Actually, i want to create multiple ball using multithreading.I have created a class 'CreateBall' and this class is making ball and second class 'Balls' is a Panel where the ball is display.But only one ball is being displayed.
Here is my code.......
CreateBall.java import java.awt.*; public class CreateBall implements Runnable { int px,py; int w=20,h=20; Graphics g; public CreateBall(int px,int py)
Below are three classes. The first and second create car objects and the third is the main method. My goal is to move two cars across the window. When I compile and run the program both cars are generated however they will not animate. I am moving them with threads but I cannot seem to find why the cars will not move.
import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class CarComponent extends JComponent { private Car car1; private Car car2;
I am very new to threading. I have a program that calls my api and gets data back in json format. Each request returns a row of data in json format. All together I need to retrieve about 2,000,000 rows a day which means 2,000,000 requests (I understand that this is bad design, but the system was not designed for this purpose it is just what I need to do for the next couple of weeks). When I tried running it on a single thread I was processing about 200 requests a minute which is much too slow. As a result I created 12 threads and I was processing 5500 rows a minutes which was a great improvement.
The problem was only about on average 90% of the rows were inserted into the database as I ran it a few times to make sure. Before each insert printed to a file each URL which was sent and then I checked to see if each insert statement was successful (returned 1 when executed ) and it all seems fine. Every time I run it it inserts about 90% but it does varies and it has never been a consistent number.
Essentially the code starts in main by creating 12 threads. Each thread's creates a run method which calls a new instance of MySQLPopulateHistData and passes a start and end integer which are used in the insert statement for ranges. I have done many system.out.println type testing and can see all the threads do start and all the 12 instances (one instance for each thread) called are executing?
MAIN:
import java.io.IOException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class MainClass { public static void main(String[] args) {