Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thread sync example and concurrent modification exception added
- Loading branch information
1 parent
3293073
commit 6fa36ec
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ThreadTest; | ||
|
||
public class ThreadCreator extends Thread | ||
{ | ||
syncobj a; | ||
int ThrdCnt=0; | ||
public ThreadCreator(syncobj a,int ThrdCnt) | ||
{ | ||
this.a = a; | ||
this.ThrdCnt = ThrdCnt; | ||
this.start(); | ||
} | ||
|
||
public void run() | ||
{ | ||
if(ThrdCnt==1||ThrdCnt==2) | ||
{ | ||
a.delayAndPrint(ThrdCnt); | ||
a.delayAndPrint2(ThrdCnt); | ||
} | ||
|
||
if(ThrdCnt==3) | ||
a.iterateArrayList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package ThreadTest; | ||
|
||
public class syncmain { | ||
|
||
/* Synchronsed method example */ | ||
|
||
public static void main(String s[]) throws InterruptedException | ||
{ | ||
syncobj obj = new syncobj(); | ||
|
||
// Synchronised method example | ||
new ThreadCreator(obj,1).join(); | ||
new ThreadCreator(obj,2).join(); | ||
|
||
// ConcrrentModificationException creator | ||
new ThreadCreator(obj,3); | ||
Thread.sleep(2000); // wait till the object is started to iterate | ||
System.out.println("adding to arraylist"); | ||
obj.al.add(3); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package ThreadTest; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
|
||
public class syncobj { | ||
|
||
ArrayList al = new ArrayList(); | ||
|
||
syncobj() | ||
{ | ||
al.add(1); | ||
} | ||
|
||
synchronized void delayAndPrint(int cnt) | ||
{ | ||
try{ | ||
System.out.print("Thread:"+cnt+",Mehod:1-Am in"); | ||
Thread.sleep(2000); | ||
System.out.println(", After 2 min out"); | ||
} | ||
catch(Exception e) | ||
{ | ||
System.out.println("Error-"+e); | ||
} | ||
} | ||
|
||
synchronized void delayAndPrint2(int cnt) | ||
{ | ||
try{ | ||
System.out.print("Thread:"+cnt+",Mehod:2-Am in"); | ||
Thread.sleep(2000); | ||
System.out.println(", After 2 min out"); | ||
} | ||
catch(Exception e) | ||
{ | ||
System.out.println("Error-"+e); | ||
} | ||
} | ||
|
||
void iterateArrayList() | ||
{ | ||
try | ||
{ | ||
System.out.println("starting iterate"); | ||
Iterator b= al.iterator(); | ||
while(b.hasNext()) | ||
{ | ||
try | ||
{ | ||
Thread.sleep(5000); | ||
b.next(); | ||
} catch (InterruptedException e) | ||
{ | ||
System.out.println(e); | ||
} | ||
} | ||
System.out.println("ending iterate"); | ||
} | ||
catch(Exception e) | ||
{ | ||
System.out.println(e); | ||
} | ||
} | ||
} |