package Package; public class Resurse { int i=0; boolean valueSet=false; void call() { System.out.println(i); } } package Package; public class NewThread_Runnable implements Runnable { Resurse s; NewThread_Runnable(Resurse s) { this.s=s; new Thread(this,"Первый поток").start(); } public void run() { try { for(int i=100;i>0;i--) { System.out.println("Первый поток " + i); synchronized(s) { s.i+=10; s.call(); } Thread.sleep(10); } } catch (InterruptedException e) { System.out.println("Первый поток прерван."); } System.out.println("Первый поток завершен."); } } package Package; public class NewThread_thread extends Thread { Resurse s; NewThread_thread (Resurse s) { // super(); // System.out.println("Поток,созд. от класса Thread "+ this); this.s=s; new Thread(this," Второй поток ").start(); } public void run() { try { for(int i=100;i>0;i--) { System.out.println("Второй поток " + i); synchronized(s) { s.i+=50; s.call(); } Thread.sleep(10); } } catch (InterruptedException e) { System.out.println("Второй поток прерван."); } System.out.println("Второй поток завершен."); } } package Package; public class Program { public static void main(String[] args) { Resurse s1=new Resurse(); new NewThread_Runnable(s1); new NewThread_thread(s1); /* try{ ob.join(); ob2.join(); }catch(InterruptedException e) { System.out.println("прервано"); } */ } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)