Java run threads again after other threads finished - java

how to run threads after other threads finished, let say i have 3 java class (Cls1 and Cls2 implements runnable, and I use sleep to know which statements are run first), this is my code :
public class Master {
#SuppressWarnings("unused")
public static void main(String[] args) {
//loop1
for(int i=1; i<=2; i++) {
Cls1 c1 = new Cls1();
}
//Here i want to wait until the thread loop1 is finished, what to do?
//loop2
for(int j=1; j<=2; j++) {
Cls2 c2 = new Cls2();
}
}
}
public class Cls1 implements Runnable{
Thread myThread;
Cls1() {
myThread = new Thread(this, "");
myThread.start();
}
#Override
public void run() {
System.out.println("hello1");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello2");
}
}
public class Cls2 implements Runnable{
Thread myThread;
Cls2() {
myThread = new Thread(this, "");
myThread.start();
}
#Override
public void run() {
System.out.println("hello3");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello4");
}
}
And this is output my code :
hello1
hello1
hello3
hello3
hello2
hello2
hello4
hello4
And this is the output I expect:
hello1
hello1
hello2
hello2
hello3
hello3
hello4
hello4
What should I do ?

You could try something like that:
#SuppressWarnings("unused")
public static void main(String[] args) {
Thread threads[] = new Thread[2];
//loop1
for(int i=1; i<=2; i++) {
threads[i-1] = new Cls1();
}
for (Thread thread: threads) {
thread.join();
}
//loop2
for(int j=1; j<=2; j++) {
Cls2 c2 = new Cls2();
}
}
UPDATE: Make Cls1 a subclass of Thread:
public class Cls1 extends Thread {
Cls1() {
start();
}
#Override
public void run() {
System.out.println("hello1");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello2");
}
}

If you want to wait for the thread to finish, call the join method.

To run thread one after another, it needs to be synchronized.
wait, notify and notifyAll ..all of these can be used.
If you don't synchronize it, then it doesn't guarantee the order of output what you desire to be produced.
So for this we have to take one variable "flag" and and synchronize threads one by one as below:
If value of flag=1, then it is class A's turn to print.
If value of flag=2, then it is class B's turn to print.
If value of flag=3, then it is class C's turn to print.

Related

java thread synchronized & lock no effect?

so much confused why I get a random result while doing 'i++' in a synchronized or a locked method?
public class aaa implements Runnable {
static int count = 0;
public static void main(String[] args) {
aaa aaa = new aaa();
aaa.create();
}
public void create() {
ExecutorService executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 1000; i++) {
aaa thread = new aaa();
executor.execute(thread);
}
executor.shutdown();
while (true){
if(executor.isTerminated()){
System.out.println("a " + count);
break;
}
}
}
#Override
public void run() {
this.test();
}
public void test() {
Lock lock = new ReentrantLock();
try {
lock.lock();
count++;
System.out.println(count);
} finally {
lock.unlock();
}
}
}
OR:
public synchronized void test() {
count++;
System.out.println(count);
}
the result is a random number sometimes 1000 sometimes 998, 999 ...etc and the print from inside the 'test' method is not in a sequence, it is like :
867
836
825
824
821
820
819
817
816
a 999
However, if it is in a synchronized block, everything looks good:
public void test() {
synchronized (aaa.class) {
count++;
System.out.println(count);
}
}
the result:
993
994
995
996
997
998
999
1000
a 1000
I think all of the methods above should give me the same result 1000, and the self increment should be in a sequence, but only the last method works.What is wrong with the code? Please help!!!
You are creating multiple instances of aaa, each instance creates its own ReentrantLock, and every thread in execution smoothly acquires a lock from its own instance.
public void test() {
Lock lock = new ReentrantLock();
try {
lock.lock();
count++;
System.out.println(count);
} finally {
lock.unlock();
}
}
Since there are multiple instances of aaa, each thread is running on its own instance and the synchronized method uses current object of aaa.class
public synchronized void test() {
count++;
System.out.println(count);
}
The reason for getting a proper result in this approach is, you are using the aaa.class as an object to the synchronization
public void test() {
synchronized (aaa.class) {
count++;
System.out.println(count);
}
}
The solution is, reuse the same lock(ReentrantLock) across all the threads. Defining the lock in the same level as the variable count would solve the issue.
You must create a single mutex, i.e.
static Lock lock = new ReentrantLock();
Your synchronized method does not work since you are creating N aaa instances then, every (non static) method is different (with their own mutex).
Your synchronized (aaa.class) works since aaa.class is the same Object for all aaa instances and methods.
Then, if you need synchronize the method be sure it is the same for all threads, e.g. if test is static will be the same for all
#Override
public void run() {
test();
}
public static synchronized void test() {
count++;
}
but you can inject a "counter class", e.g.
class Counter {
int count = 0;
// non static but synchronized for all (since they use the same `counter` object)
synchronized void inc() {
count++;
}
}
to be used for all threads
...
SyncTest thread = new SyncTest(counter); // <== the same
...
(full code)
public class SyncTest implements Runnable {
private final Counter c;
public SyncTest(Counter c) {
this.c = c;
}
static class Counter {
int count = 0;
// non static but synchronized for all (since they use the same `counter` object)
synchronized void inc() {
count++;
}
}
#Override
public void run() {
test();
}
public void test() {
this.c.inc();
}
public static void main(String[] args) {
// one counter for all
Counter counter = new Counter();
ExecutorService executor = Executors.newFixedThreadPool(100);
for (int i = 0; i < 10000; i++) {
SyncTest thread = new SyncTest(counter);
executor.execute(thread);
}
executor.shutdown();
while (true) {
if (executor.isTerminated()) {
System.out.println("a " + counter.count);
break;
}
}
}
}
Rule of thumb: Declare your lock variable on the next line after the variable(s) that you want to protect with it, and declare it with the same keywords. E.g.,
public class aaa implements Runnable {
static int count = 0;
static Lock countLock = new ReentrantLock();
...
If you read deeply enough into any of the other answers here, then you will see why this helps.

Alternate printing multithreading

Here is my simple code for 2 threads to alternate in printing 2 different numbers. Thread A prints 1 and Thread B prints 2. Both should alternate amongst themselves to print 1,2,1,2,1,2,1,2,....and so on.
Now I see in the below program we are getting <1,2>,<1,2> combination,...and so on for 5 times only.
Looks like both threads have the same variables which is being shared. But i is a local variable. How it i shared
public class MyThread extends Thread{
int numToPrint;
Turn turn;
MyThread(int n, Turn turn)
{
this.numToPrint=n;
this.turn=turn;
}
public void run() {
for (int i=0;i<10;i++) {
if(turn.t==numToPrint) {
System.out.println(numToPrint);
if(numToPrint==1)
turn.t=2;
else
turn.t=1;
turn.doNotify();
}
else
{
turn.doWait();
}
}
}
}
public class Main {
public static void main(String[] args) {
Turn t=new Turn();
MyThread t1=new MyThread(1,t);
t1.start();
MyThread t2=new MyThread(2,t);
t2.start();
}
}

Why is Java synchronized not working as expected?

I'm trying to figure out how synchronized methods work. From my understanding I created two threads T1 and T2 that will call the same method addNew, since the method is synchronized shouldn't it execute all the iterations of the for loop for one thread and then the other? The output keeps varying, sometimes it prints it right, other times it prints values from T1 mixed with T2 values. The code is very simple, can someone point out what am I doing wrong? Thank you.
public class Main {
public static void main(String[] args) {
Thread t1 = new Thread(new A());
Thread t2 = new Thread(new A());
t1.setName("T1");
t2.setName("T2");
t1.start();
t2.start();
}
}
public class B {
public synchronized void addNew(int i){
Thread t = Thread.currentThread();
for (int j = 0; j < 5; j++) {
System.out.println(t.getName() +"-"+(j+i));
}
}
}
public class A extends Thread {
private B b1 = new B();
#Override
public void run() {
b1.addNew(100);
}
}
Each A instance has its own B instance. The method addNew is an instance method of B. Therefore, the lock acquired implicitly during calls to addNew is the lock on the receiver B instance. Each thread is calling addNew on a different B, and therefore locking on different locks.
If you want all B instances to use a common lock, create a single shared lock, and acquire it in the body of addNew.
Both A objects have their own B object. You need them to share a B so the synchronization can have an effect.
try this :
public class Main {
public static void main(String[] args) {
A a = new A();
Thread t1 = new Thread(a);
Thread t2 = new Thread(a);
t1.setName("T1");
t2.setName("T2");
t1.start();
t2.start();
}
}
class B {
public synchronized void addNew(int i){
Thread t = Thread.currentThread();
for (int j = 0; j < 5; j++) {
System.out.println(t.getName() +"-"+(j+i));
}
}
}
class A extends Thread {
private B b1 = new B();
#Override
public void run() {
b1.addNew(100);
}
}

How to execute three thread simultaneously?

This is my code which i tried but my main class is not there because i don't know how to use that one
//first thread
class firstthread extends Thread
{
public void run(){
for(int i=0;i<1000;i++)
{
System.out.println(i);
}}
}
//second thread
class secondthread extends Thread
{
public void run(){
for(int i=0;i<1000;i++)
{
System.out.println(i);
}}
}
First overide the run method and then create the object of thread class in main()
and call start method.
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
new Thread() {
public void run() {
for(int y=0;y<1000;y++)
{
System.out.println(y);
}
};
}.start();
}
}
Whatever you have written is incomplete code, to create a thread you need to extend Thread class or implement Runnable interface and then override its public void run() method.
To create a thread you need to override the method public void run
Then to start the threads you need to call its start() method.
A simple complete example
class MyThread extends Thread {
String name;
public void run(){
for(int i=0;i<1000;i++) {
System.out.println("Thread name :: "+name+" : "i);
}
}
}
class Main{
public static void main(String args[]){
MyThread t1 = new MyThread();
t1.name = "Thread ONE";
MyThread t2 = new MyThread();
t2.name = "Thread TWO";
MyThread t3 = new MyThread();
t3.name = "Thread THREE";
t1.start();
t2.start();
t3.start();
}
}
You can't just put some code in your class body.
You need a method to have the code in, the method being run() in case of thread.
Instead of copy-pasting the code, I'll point you to the official documentation where you can find some examples.
Sample program given below. Since there is no synchronization code, there output is mixed from the three threads
public class ThreadTest implements Runnable{
#Override
public void run() {
System.out.print(Thread.currentThread().getId() + ": ");
for(int i=0;i<100;i++)
System.out.print(i + ", ");
System.out.println();
}
public static void main(String[] args) {
for(int i=0;i<3;i++){
new Thread(new ThreadTest()).start();
}
}
}

Synchronized two threads not working synchronized

consider the following code:
public class Main {
public static void main(String[] args){
MyObject obj = new MyObject();
Thread1 t1 = new Thread1(100,'#',obj);
Thread1 t2 = new Thread1(100,'*',obj);
t1.start();
t2.start();
}
}
public class Thread1 extends Thread {
int myNum;
char myChar;
MyObject myObj;
public Thread1(int num, char c, MyObject obj){
myNum = num;
myChar = c;
myObj = obj;
}
public synchronized void run(){
for(int i = 1; i<myNum; i++){
if((i%10)==0)
System.out.println("");
System.out.print(myChar);
}
}
}
The class MyObject is empty class without anything spacial.
My question is, why the synchronized is not working and I get printed the '#' and '' simultaneously and in random order and not one after the other?
Synchronization locks the monitor of an object. In your case, you are synchronizing against each of the threads objects, that is Thread A locks against Threads A's monitor and Thread B locks against Thread B's monitor. Thus they are not interacting.
I think that you meant the following for the run() method:
public void run(){
synchronized (myObj) {
for(int i = 1; i<myNum; i++){
if((i%10)==0)
System.out.println("");
System.out.print(myChar);
}
}
}
If that was indeed your intended use for myObj, then I would suggest the following change too; as it will help make the code more readable.
Change:
MyObject myObj;
to:
private final MyObject lock;
EDIT an alternative approach, that does not lock against myObj but instead the Thread1's instance of Class.
public void run(){
doJob();
}
private static synchronized void doJob() {
for(int i = 1; i<myNum; i++){
if((i%10)==0)
System.out.println("");
System.out.print(myChar);
}
}
You can try to put thread.join(), wait for first thread in execution finished to start next one
public class hilos {
public static void main(String[] args) throws InterruptedException{
MyObject obj = new MyObject();
Thread1 t1 = new Thread1(50,'#',obj);
Thread1 t2 = new Thread1(50,'*',obj);
Thread[] threads = {t1, t2};
start(threads);
}
public synchronized static void start(Thread[] threads) throws InterruptedException{
synchronized(threads){
for(int i=0; i < threads.length; i++) {
threads[i].start();
threads[i].join();
}
}
}
}
OUTPUT:
#########
##########
##########
##########
##########*********
**********
**********
**********
**********

Categories

Resources