[2017-08-24T02:16:04,076][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [10] uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:127) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:114) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:67) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:122) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.cli.Command.main(Command.java:88) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84) ~[elasticsearch-5.5.2.jar:5.5.2] Caused by: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:106) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:194) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:351) ~[elasticsearch-5.5.2.jar:5.5.2] at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:123) ~[elasticsearch-5.5.2.jar:5.5.2] ... 6 more
wget -c https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz mv mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz /usr/local cd /usr/local tar zxf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz && mv mysql-5.7.19-linux-glibc2.12-x86_64 mysql cd mysql chown -R mysql:mysql .
/** * Atomically sets the value to the given updated value * if the current value {@code ==} the expected value. * * @param expect the expected value * @param update the new value * @return {@code true} if successful. False return indicates that * the actual value was not equal to the expected value. */ publicfinalbooleancompareAndSet(boolean expect, boolean update){ int e = expect ? 1 : 0; int u = update ? 1 : 0; return unsafe.compareAndSwapInt(this, valueOffset, e, u); }
我们可以看到compareAndSwapInt其实就是CAS(比较与交换,Compare and swap)无锁算法 当多个线程尝试使用CAS同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是被告知这次竞争中失败,并可以再次尝试
publicstaticvoidmain(String[] args){ new Thread(() -> { int i = 0; while (flag) { i++; } System.out.println(Thread.currentThread().getName() + ":线程结束"); }).start();
publicstaticvoidmain(String[] args){ new Thread(() -> { int i = 0; while (flag) { i++; } System.out.println(Thread.currentThread().getName() + ":线程结束"); }).start();
privatestaticfinal Unsafe theUnsafe = new Unsafe();
/** * Provides the caller with the capability of performing unsafe * operations. * * <p> The returned <code>Unsafe</code> object should be carefully guarded * by the caller, since it can be used to read and write data at arbitrary * memory addresses. It must never be passed to untrusted code. * * <p> Most methods in this class are very low-level, and correspond to a * small number of hardware instructions (on typical machines). Compilers * are encouraged to optimize these methods accordingly. * * <p> Here is a suggested idiom for using unsafe operations: * * <blockquote><pre> * class MyTrustedClass { * private static final Unsafe unsafe = Unsafe.getUnsafe(); * ... * private long myCountAddress = ...; * public int getCount() { return unsafe.getByte(myCountAddress); } * } * </pre></blockquote> * * (It may assist compilers to make the local variable be * <code>final</code>.) * * @exception SecurityException if a security manager exists and its * <code>checkPropertiesAccess</code> method doesn't allow * access to the system properties. */ publicstatic Unsafe getUnsafe(){ Class cc = sun.reflect.Reflection.getCallerClass(2); if (cc.getClassLoader() != null) thrownew SecurityException("Unsafe"); return theUnsafe; }
控制台输出: Exception in thread "main" java.lang.SecurityException: Unsafe at sun.misc.Unsafe.getUnsafe(Unsafe.java:90) at com.java.util.concurrent.AtomicBooleanDemo.main(AtomicBooleanDemo.java:11)
为什么会抛出这个异常呢,注意看Class cc = sun.reflect.Reflection.getCallerClass(2);这行代码 这里定义了只有主类加载器加载的类才能调用这个方法, 否者抛出异常