I am trying to learn about threads and synchronization. I made this test program:
public class Test { static List<Thread> al = new ArrayList<>(); public static void main(String[] args) throws IOException, InterruptedException { long startTime = System.currentTimeMillis(); al.add(new Thread(() -> fib1(47))); al.add(new Thread(() -> fib2(47))); for (Thread t : al) t.start(); for (Thread t: al) t.join(); long totalTime = System.currentTimeMillis() - startTime; System.out.println(totalTime); } public static synchronized int fib1(int x) { return x <= 2 ? 1 : fib1(x-2) + fib1(x-1); } public static synchronized int fib2(int x) { return x <= 2 ? 1 : fib2(x-2) + fib2(x-1); } }
This program takes about 273 seconds, but if I remove both of it synchronized, it will work in 7 seconds. What makes this huge difference?
synchronized
EDIT: I know that I'm using a terribly slow Fibonacci number calculation algorithm. And I also know that threads do not share resources, and therefore methods do not need to be synchronized. However, this is just a test program in which I try to understand how it works synchronized, and I chose a slow algorithm so that I can measure the time spent in milliseconds.
static synchronized , , , , ( Test). . , fib , . .
static synchronized
, , . fib, , , , .
, , , fib, . , , , , , .
- , . , , fib1 fib2 .
, , , . , , , , , , .
- . :
1.
, , , . . .
2.
:
, synchronized , . , static, . , , .
static
synchronized , , , , . , , . - - - , .
, , - synchronized. - , JVM. , JVM synchronized, . - , synchronized - " " . , , JVM , synchronized, , .
Btw: fib1 fib2 ,
@MartinS , , . , .
. , , 100%, , . , - , 100%. , . , .
, ( , ). , .
, - . "" - .
N >= 4, . , , N- , 2 ^ N . 2 ^ 47 - (, ). , , , .
. Java, , , . , , . :)
fib1 ( fib2) , . , ( , ). , synchronized Java .
fib1
fib2
.
private
public
, 14 , Test.class.
Test.class
, , , . , Test.class.
Test.class.
static final Object LOCK1 = new Object(); static final Object LOCK2 = new Object();
fib1() fib2() . .
fib1()
fib2()
public static int fib1(int x) { synchronized(LOCK1) { return x <= 2 ? 1 : fib1(x-2) + fib1(x-1); } } public static int fib2(int x) { synchronized(LOCK2) { return x <= 2 ? 1 : fib2(x-2) + fib2(x-1); } }
LOCK1, , LOCK2, , . ( ). , .
LOCK1
LOCK2
Source: https://habr.com/ru/post/1628033/More articles:TSify ignores conversions during Browserify - javascriptUsing vs partial component for website header in ember.js - ember.jsЕсли значения Hidden Filed изменяются, кнопка Save должна быть включена. Если введено такое же значение, кнопка "Сохранить" снова должна быть отключена - jqueryDoes an Android application collect an application on another computer Create a new signature certificate? - androidhttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1628032/how-do-i-pass-command-line-arguments-to-python-from-vs-in-debug-mode&usg=ALkJrhiKnzCB6ELtZGKq_1CWo_aK2Yx7pw3 divs on one line inside another div - htmlHow to get ClaimsPrincipal from ADAL AuthenticationResult - claims-based-identityFrangi filter acting as an edge detector - image-processingLoop through an array at given intervals - jquerySimultaneous maintenance of C ++ 98 and C ++ 11 - c ++All Articles