Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- synchronized public static void threadFunc() {
- newLine = true;
- x = 0; y = 0;
- Thread t1 = new Thread(){
- @Override
- public void run() {
- while (x == 0) {
- try {
- Thread.sleep((long)0.01);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.print("a");
- y = 1;
- y = 0;
- System.out.print("d");
- y = 1;
- synchronized(this) {
- if (newLine) {
- System.out.print("\n");
- newLine = false;
- }
- }
- }
- };
- Thread t2 = new Thread(){
- @Override
- public void run() {
- System.out.print("b");
- x = 1;
- while (y == 0) {
- try {
- Thread.sleep((long)0.01);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.print("c");
- synchronized(this) {
- if (newLine) {
- System.out.print("\n");
- newLine = false;
- }
- }
- }
- };
- t1.start();
- t2.start();
- try {
- t1.join();
- t2.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement