Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


ravi
multi threads
1 Answer(s)      5 years and 9 months ago
Posted in : Java Beginners



Advertisement
View Answers

April 16, 2009 at 10:38 AM


Hi friend,

Code to help in solving the problem :

public class SynchronizedMethod extends Object {
private static int count = 1;
public static synchronized int getCount() {
int i = count;
count++;
return i;
}
private static void print(String msg) {
String threadName = Thread.currentThread().getName();
System.out.println(threadName + ": " + msg);
}
public static void main(String[] args) {
try {
Runnable runnable = new Runnable() {
public void run() {
System.out.println("count=" + getCount());
}
};
Thread threadA = new Thread(runnable, "ThreadA");
threadA.start();
Thread.sleep(500);
Thread threadB = new Thread(runnable, "ThreadB");
threadB.start();
Thread.sleep(500);
Thread threadC = new Thread(runnable, "ThreadC");
threadC.start();
Thread.sleep(500);
Thread threadD = new Thread(runnable, "ThreadD");
threadD.start();
} catch(Exception x ) {}
}
}

Thanks


Related Tutorials/Questions & Answers:
multi threads - Java Beginners
multi threads  Hi i writing a multi threaded program in java .I m using three threads. I want to declare variables which will be available to all the threads to access. Is there a way to declare the variables as global variables
regardoing multi threads - Java Beginners
regardoing multi threads  Hi Please tell me how to declare global variables in main thread so that all other threads can use them and value will be available to all threads. Thanks
Advertisements
implementing an algorithm using multi threads - Java Beginners
implementing an algorithm using multi threads  Hi i need to implement an algorith in multi threads.Algorithm has data dependency so i need to pass... to breakdown into two or three threads and need to implemented and need
threads
threads  what are threads? what is the use in progarmming
Multi Threading
Multi Threading  Hi... I need to solve the following question: Input an array of 10 integers. Separate the odd numbers, even numbers, positive numbers and negative numbers by different threads. I tried to solve it but I dont
threads and events
threads and events  Can you explain threads and events in java for me. Thank you.   Java Event Handling Java Thread Examples
threads in java
threads in java  how to read a file in java , split it and write into two different files using threads such that thread is running twice
multi threaded program - Java Beginners
multi threaded program  Hi i m developing a multi thread program to execute a real time algorith. I m using three threads. I want to share date between three threads .Is there a way to share data from one thread to another thread
Java threads
Java threads  What are the two basic ways in which classes that can be run as threads may be defined
multi thread
multi thread   Multiple Clients Connect to a server and communicate among the themselves. When a new Client is connected to the server, all other clients get the notification and they can chat with each other. Similarly when
multi language
multi language  Hello friends please help me Hi how can i do a java program using swing that changes the language like malayalam/tamil etc with languages on a drop down menu I am using netbeans IDE
disadvantage of threads
is the disadvantage of threads?   hello, The Main disadvantage of in threads... disadvantage of Threads. Let?s discuss the disadvantages of threads. The global..., the other threads using the same memory location will be killed automatically
Multi window
Multi window  I am making a bank app using struts 1.2. using only the struts tags and nno jsp tags and scriplets. db connected thru dao. 1st page contains all the details. when i submit this page, a new window opens and asks
multi dimension
multi dimension  class TwoDAgain{ public static void main(String args[]){ int twoD[][]=new int[4][]; twoD[0]= new int[1]; twoD[1]= new int[2]; twoD[2]= new int[3]; twoD[3]= new int[4]; int i, j, k = 0; for (i=0; i<4; i
Sync Threads
Sync Threads  "If two threads wants to execute a synchronized method in a class, and both threads are using the same instance of the class to invoke the method then only one thread can execute the method at a time." This is what
threads in java
threads in java  iam getting that the local variable is never read in eclipse in main classas:: class Synex4{ public static void main(String args[]){ Test1 ob1=new Test1(); //local variable never read
Multi-Threading - Java Beginners
Multi-Threading  Consider a producer process prime-generator produces prime numbers randomly that are displayed by a consumer process called prime... threads for the producer/consumer processes and call them randomly
Java multi-threading
Java multi-threading  What is multi-threading
multi threading code - Java Interview Questions
multi threading code  Hi this is Anji.My question is how to share the variable between two threads?.Can any one post the answer please
what is multi-threading .?
what is multi-threading .?   what is multi-threading.what is the use in real time, with example programs
Threads in realtime projects
Threads in realtime projects  Explain where we use threads in realtime projects with example
Synchronized Threads
Synchronized Threads       In Java, the threads are executed independently to each other. These types of threads are called as asynchronous threads. But there are two problems may
Coding for life cycle in threads
Coding for life cycle in threads  program for life cycle in threads
Java multi-threading
Java multi-threading  How does multithreading take place on a computer with a single CPU
Java multi-threading
Java multi-threading  What method is invoked to cause an object to begin executing as a separate thread
Advertisements
 

 

 

DMCA.com