Advertisement
Guest User

Java Ppr

a guest
Dec 6th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.73 KB | None | 0 0
  1. //1st
  2. package First;
  3.  
  4. public class App {
  5. private static int counter = 0;
  6.  
  7. public static void main(String[] args) {
  8. process();
  9. System.out.println(counter);
  10.  
  11. }
  12.  
  13. public static void process() {
  14. Thread t1 = new Thread(new Runnable() {
  15.  
  16. public void run() {
  17. for (int i = 0; i < 1000; i++) {
  18. counter++;
  19. }
  20. }
  21. });
  22.  
  23. Thread t2 = new Thread(new Runnable() {
  24.  
  25. public void run() {
  26. for (int i = 0; i < 1000; i++) {
  27. counter++;
  28.  
  29. }
  30. }
  31. });
  32.  
  33. t1.start();
  34. t2.start();
  35. try {
  36. t1.join();
  37. t2.join();
  38. } catch (InterruptedException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42.  
  43. }
  44. }
  45. //2nd
  46. package Second;
  47.  
  48. public class App {
  49. private static int counter=0;
  50.  
  51. public static void main(String[] args) {
  52. process();
  53. System.out.println(counter);
  54.  
  55. }
  56. public synchronized static void increment(){
  57. counter++;
  58. }
  59. public static void process(){
  60. Thread t1= new Thread(new Runnable(){
  61.  
  62. public void run(){
  63. for (int i=0;i<1000000000;i++){
  64.  
  65. increment();
  66.  
  67. }
  68. }
  69. });
  70.  
  71. Thread t2= new Thread(new Runnable(){
  72.  
  73. public void run(){
  74. for (int i=0;i<1000000000;i++){
  75.  
  76. increment();
  77. }
  78. }
  79. });
  80.  
  81. t1.start();
  82. t2.start();
  83. try {
  84. t1.join();
  85. t2.join();
  86. } catch (InterruptedException e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. }
  90.  
  91. }
  92. }
  93.  
  94. //3rd
  95.  
  96. package Third;
  97.  
  98. public class App {
  99. public static int count1 = 0;
  100. public static int count2 = 0;
  101.  
  102. public static void main(String[] args) {
  103. Thread t1 = new Thread(new Runnable() {
  104.  
  105. public void run() {
  106.  
  107. compute();
  108. }
  109. });
  110. Thread t2 = new Thread(new Runnable() {
  111.  
  112. public void run() {
  113.  
  114. compute();
  115.  
  116. }
  117. });
  118. Thread t3 = new Thread(new Runnable() {
  119.  
  120. public void run() {
  121.  
  122. compute();
  123. }
  124. });
  125. t1.start();
  126. t2.start();
  127. t3.start();
  128. try {
  129. t1.join();
  130. t2.join();
  131. t3.join();
  132. } catch (InterruptedException e) {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. }
  136. System.out.println("Count1=" + count1 + " - Count2=" + count2);
  137. }
  138.  
  139. public static synchronized void add() {
  140. count1++;
  141. }
  142.  
  143. public static void addAgain() {
  144. count2++;
  145. }
  146.  
  147. public static void compute() {
  148. for (int i = 0; i < 100000; i++) {
  149. add();
  150. addAgain();
  151. }
  152. }
  153.  
  154. }
  155.  
  156. //4th
  157.  
  158. package Fourth;
  159.  
  160. public class App {
  161. private static int count1 = 0;
  162. private static int count2 = 0;
  163. private static Object lock1 = new Object();
  164. private static Object lock2 = new Object();
  165.  
  166. public static void main(String[] args) {
  167. Thread t1 = new Thread(new Runnable() {
  168. public void run() {
  169. incrementCount1();
  170.  
  171. }
  172. });
  173. Thread t2 = new Thread(new Runnable() {
  174. public void run() {
  175. incrementCount2();
  176. }
  177. });
  178.  
  179. t1.start();
  180. t2.start();
  181.  
  182. }
  183.  
  184. public static void incrementCount1() {
  185. synchronized (lock1) {
  186. for (int i = 0; i < 10000000; i++) {
  187. count1++;
  188. System.out.println("Count1= " + count1);
  189.  
  190. }
  191. }
  192. }
  193.  
  194. public static void incrementCount2() {
  195. synchronized (lock2) {
  196. for (int i = 0; i < 10000000; i++) {
  197. count2++;
  198. System.out.println("Count2= " + count2);
  199. }
  200. }
  201. }
  202. }
  203.  
  204. //5th
  205. package Fifth;
  206.  
  207. class Processor {
  208.  
  209. public void produce() throws InterruptedException {
  210. synchronized (this) {
  211. System.out.println("We are in producer method ...");
  212. wait();
  213. System.out.println("Again producer method....");
  214.  
  215. }
  216.  
  217. }
  218.  
  219. public void consume() throws InterruptedException {
  220. Thread.sleep(3000);
  221. synchronized (this) {
  222. System.out.println("Consumer method.....");
  223. notify();
  224.  
  225. }
  226.  
  227. }
  228.  
  229. }
  230.  
  231. public class App {
  232.  
  233. public static void main(String[] args) {
  234. Processor processor=new Processor();
  235.  
  236. Thread t1 = new Thread(new Runnable() {
  237. public void run() {
  238. try {
  239. processor.produce();
  240. } catch (InterruptedException e) {
  241. // TODO Auto-generated catch block
  242. e.printStackTrace();
  243. }
  244. }
  245.  
  246. });
  247.  
  248. Thread t2 = new Thread(new Runnable() {
  249. public void run() {
  250. try {
  251. processor.consume();
  252. } catch (InterruptedException e) {
  253. // TODO Auto-generated catch block
  254. e.printStackTrace();
  255. }
  256.  
  257. }
  258.  
  259. });
  260.  
  261. t1.start();
  262. t2.start();
  263.  
  264. try {
  265. t1.join();
  266. t2.join();
  267. } catch (InterruptedException e) {
  268. // TODO Auto-generated catch block
  269. e.printStackTrace();
  270. }
  271. }
  272.  
  273. }
  274.  
  275.  
  276. //6th
  277.  
  278. package Sixth;
  279.  
  280. import java.util.ArrayList;
  281. import java.util.List;
  282.  
  283. class Processor {
  284.  
  285. private List<Integer> list = new ArrayList<>();
  286. private final int LIMIT = 5;
  287. private final int BOTTOM = 0;
  288. private final Object lock = new Object();
  289. private int value = 0;
  290.  
  291. public void producer() throws InterruptedException {
  292. synchronized (lock) {
  293. while (true) {
  294. if (list.size() == LIMIT) {
  295. System.out.println("Waiting for removing items from the list...");
  296. lock.wait();
  297. } else {
  298. System.out.println("Adding: " + value);
  299. list.add(value);
  300. value++;
  301. lock.notify();
  302. }
  303. Thread.sleep(500);
  304. }
  305.  
  306. }
  307.  
  308. }
  309.  
  310. public void consumer() throws InterruptedException {
  311. synchronized (lock) {
  312. while (true) {
  313. if (list.size() == BOTTOM) {
  314. System.out.println("Waiting for adding items to the list...");
  315. lock.wait();
  316. } else {
  317. System.out.println("Removed: " + list.remove(--value));
  318. lock.notify();
  319. }
  320. Thread.sleep(500);
  321.  
  322. }
  323.  
  324. }
  325.  
  326. }
  327.  
  328. }
  329.  
  330. public class App {
  331.  
  332. public static void main(String[] args) {
  333. Processor processor = new Processor();
  334.  
  335. Thread t1 = new Thread(new Runnable() {
  336. public void run() {
  337. try {
  338. processor.producer();
  339. } catch (InterruptedException e) {
  340. // TODO Auto-generated catch block
  341. e.printStackTrace();
  342. }
  343. }
  344.  
  345. });
  346.  
  347. Thread t2 = new Thread(new Runnable() {
  348. public void run() {
  349. try {
  350. processor.consumer();
  351. } catch (InterruptedException e) {
  352. // TODO Auto-generated catch block
  353. e.printStackTrace();
  354. }
  355.  
  356. }
  357.  
  358. });
  359. t1.start();
  360. t2.start();
  361. try {
  362. t1.join();
  363. t2.join();
  364. } catch (InterruptedException e) {
  365. // TODO Auto-generated catch block
  366. e.printStackTrace();
  367. }
  368. }
  369.  
  370. }
  371.  
  372. //7th
  373.  
  374. package MethodSyn;
  375.  
  376. public class App {
  377. public static int count1 = 0;
  378. public static int count2 = 0;
  379.  
  380. public static void main(String[] args) {
  381. Thread t1 = new Thread(new Runnable() {
  382. public void run() {
  383. incrementCount1();
  384. }
  385. });
  386.  
  387. Thread t2 = new Thread(new Runnable() {
  388. public void run() {
  389. incrementCount2();
  390.  
  391. }
  392. });
  393.  
  394. t1.start();
  395. t2.start();
  396.  
  397. }
  398.  
  399. public synchronized static void incrementCount1() {
  400. for(int i=0;i<1000;i++) {
  401. count1++;
  402. System.out.println("Count1= "+ count1);
  403. }
  404. }
  405.  
  406. public synchronized static void incrementCount2() {
  407. for(int i=0;i<1000;i++) {
  408. count2++;
  409. System.out.println("Count2= "+ count2);
  410. }
  411. }
  412. }
  413.  
  414. //8th
  415. package MethodSyn2;
  416.  
  417. public class App {
  418. public static int count1 = 0;
  419. public static int count2 = 0;
  420.  
  421. public static void main(String[] args) {
  422. Thread t1 = new Thread(new Runnable() {
  423. public void run() {
  424. incrementCount1();
  425. }
  426. });
  427.  
  428. Thread t2 = new Thread(new Runnable() {
  429. public void run() {
  430. incrementCount2();
  431.  
  432. }
  433. });
  434.  
  435. t1.start();
  436. t2.start();
  437.  
  438. }
  439.  
  440. public static void incrementCount1() {
  441. synchronized (App.class) {
  442. for (int i = 0; i < 1000; i++) {
  443. count1++;
  444. System.out.println("Count1= " + count1);
  445. }
  446. }
  447. }
  448.  
  449. public static void incrementCount2() {
  450. synchronized (App.class) {
  451. for (int i = 0; i < 1000; i++) {
  452. count2++;
  453. System.out.println("Count2= " + count2);
  454. }
  455. }
  456. }
  457.  
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement