Java多线程同步的几种方式

1、原子变量
AtomicBoolean
AtomicInteger
AtomicLong
AtomicReference
AtomicStampedReference
AtomicIntegerArray
AtomicLongArray
AtomicReferenceArray

2、Unsafe类
Unsafe.monitorEnter(this);
Unsafe.monitorExit(this);

3、synchronized关键字
同步静态方法
同步普通方法
同步代码块

4、锁
Object.wait()方法和Object.notify()方法:

5、ReentrantLock重入锁
Condition

6、ReadWriteLock读写锁
StampedLock

7、Semaphore信号量

8、BlockingQueue阻塞队列
ArrayBlockingQueue
LinkedBlockingQueue
PriorityBlockingQueue
LinkedBlockingDeque
SynchronousQueue
DelayQueue

9、CyclicBarrier

10、CountDownLatch

11、Phaser

12、Exchanger

13、ForkJoinPool
RecursiveTask

13、Future
CompletionService
CompletableFuture
FutureTask

14、Collections
Collections.synchronizedList
Collections.synchronizedSet
Collections.synchronizedMap

15、volatile变量
可以解决并发读的问题,无法单独解决并发写的问题

Java多线程实现的几种方式

1、继承Thread类

2、实现Runnable接口

3、实现Callable接口+Future封装

4、定时器

5、使用Executors提供的ExecutorService
Executors.newCachedThreadPool
Executors.newFixedThreadPool
Executors.newScheduledThreadPool
Executors.newSingleThreadExecutor
Executors.newWorkStealingPool

6、自定义ThreadPoolExecutor+ThreadFactory

7、parallelStream

8、Fork Join
ForkJoinPool
RecursiveTask
RecursiveAction

9、调用本地方法
使用JNI等

跨线程创建窗体的两种方法

1、利用新STA线程进行创建

//线程
private System.Threading.Thread th=null;

//创建线程
try
{
    th = new System.Threading.Thread((System.Threading.ThreadStart)delegate
    {
        Application.Run(new fmFreeChatClient(url));
    });

    th.SetApartmentState(ApartmentState.STA);
    th.Start();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

2、用delegate

//在form中声明
#region 新建Form并显示
delegate void showForm(string url);
public void showMyForm(string url)
{
//新建窗体并显示
}
#endregion

//在线程中调用
Object[] objs = new Object[1];
objs[0]="hello";
this.Invoke(new showForm(showMyForm),objs);

用VC编译Nginx

1、安装Mysis和VC
2、用svn从Nginx取回源代码
3、调用下面的bat

@call "D:\Microsoft Visual Studio 9.0\VC\bin\vcvars32.bat"
@call "D:\GNUstep\msys.bat"
cmd

4、Config

./auto/configure --prefix= --sbin-path=nginx --with-cc=cl --with-cc-opt="" --without-http_rewrite_module --without-http_gzip_module

5、Make

nmake

JS中null与undefined

1.undefined是js的一个原始值,不是一个对象,表示变量没有初始化,
null也是js的一个原始值,不是一个对象,他表示变量为空,但由于语言设计的缺陷,导致typeof(null)为object,而正确的应该为null

alert(typeof(null));      // object
alert(typeof(undefined)); // undefined

alert(null !== undefined); //true
alert(null == undefined);  //true

2.object == null 与 !object 是不同的,因为!object相当于!Boolean(object)

alert(Boolean(null)); //false
alert(Boolean(0));    //false
alert(Boolean(""));   //false
alert(Boolean({}));   //true
alert(Boolean([]));   //true

双击运行的jar包

让jar包可以双击运行,其实很简单

1.写一个manifest.mf文件,里面只有两行
第一行为Main-Class
第二行为空行
注意冒号后面有一个空格

Main-Class: YourClassName

2.用jar命令打包

jar -cvfm xxx.jar xxx.mf classfiles

3.然后就可以运行了

要注意,双击运行jar包时,
系统会调用javaw而不是java,
因此命令行输出是看不到的。

JNI简单例子1

1.Java代码CallApi.java

class CallApi{
    private native String showMessageBox(String msg);
    private native double getRandomDouble();

    static{
        try{
            System.loadLibrary("CallApi");
            System.out.println("Loaded CallApi");
        }catch(UnsatisfiedLinkError e){
            //nothing to do
            System.out.println("Couldn't load CallApi");
            System.out.println(e.getMessage());
        }
    }

    public static void main(String args[]){
        CallApi api = new CallApi();
        double randomNumber = api.getRandomDouble();
        String retval = api.showMessageBox("Hello from Java!\n"+
            "The native random number: "+randomNumber);
            System.out.println("The native string: "+retval);
    }
}

2.生成dll的CallApi.h文件

javah -classpath . -jni CallApi
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class CallApi */

#ifndef _Included_CallApi
#define _Included_CallApi
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     CallApi
 * Method:    showMessageBox
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_CallApi_showMessageBox
  (JNIEnv *, jobject, jstring);

/*
 * Class:     CallApi
 * Method:    getRandomDouble
 * Signature: ()D
 */
JNIEXPORT jdouble JNICALL Java_CallApi_getRandomDouble
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

3.手写dll的CallApi.c文件

#include"CallApi.h"
#include <windows.h>
#include <time.h>

//#pragma comment(lib,"user32.lib")

JNIEXPORT jstring JNICALL Java_CallApi_showMessageBox
  (JNIEnv *env, jobject thisObject, jstring js)
{
    //first convert jstring to const char for use in MessageBox
    const jbyte* argvv = (*env)->GetStringUTFChars(env, js, NULL);
    char* argv =(char *) argvv;

    //Call MessageBoxA
    MessageBox(NULL, argv, "Called from Java!", MB_ICONEXCLAMATION | MB_OK);
    return js;

}


JNIEXPORT jdouble JNICALL Java_CallApi_getRandomDouble
  (JNIEnv *env, jobject thisObject)
{
    double num1;
    srand((unsigned)(time(0)));
    num1 = ((double)rand()/(double)RAND_MAX);

    return num1;
}

4.编译
注意1:要引用jdk下的include目录下文件,因此要增加引用文件路径
注意2:用MT而不是MD

5.运行

java -Djava.library.path=. CallApi