二、用vscode打开创建好的项目
三、在vscode中安装几个小程序插件即可
1、搜索安装vscode weapp api,这是小程拦禅序的语法结构api
2、安装vscode wxml这个插件,主要是针对wxml文件的
3、安装vscode-wechat这个插件
4、安装一下Easy WXLESS,是针携伍对wxss文件的
以上安装好插件之后,在vscode里面编辑小程序项目,保存即可同步到小程序开发工具中简隐尘啦!
这样在vscode中也可以使用小程序的api啦~
前面说到了Java中的同步问题 下面通过一个小小的实例程序来演示Java中的同步方法 其中对前文提到的Counter类做了稍微的修改
public class Counter {
private int c =
public void increment() {
System out println( before increment c = + c)
c++
try {
Thread sleep( )
} catch (InterruptedException e) {
e printStackTrace()
}
System out println( after increment c = + c)
}
public void decrement() {
System out println( before decrement c = + c)
c
try {
Thread sleep( )
} catch (InterruptedException e) {
e printStackTrace()
}
System out println( after decrement c = + c)
}
public int value() {
return c
}
}
在上面的Counter类的实现中 分别对increment和decrement方法中增加了sleep( )的调用 这样做的目的是为了放大两个线程饥察对同一对象的方法调用时的交错效果
下面是两个线程 在ThreadA中调用了 次increment()方法 在ThreadB中调用了 次decrement()方法
Thread
public class ThreadA implements Runnable {
private Counter c
public ThreadA(Counter c) {
this c = c
}
@Override
public void run() {
for (int i = i <i++) {
this c increment()
}
}
}
凯竖ThreadB
public class ThreadB implements Runnable {
private Counter c
public ThreadB(Counter c) {
盯肢大 this c = c
}
@Override
public void run() {
for (int i = i <i++) {
this c decrement()
}
}
}
主程序如下 其中生成了两个线程threadA和ThreadB 他们共享Counter c
public class Main {
public static void main(String[] args) {
Counter c = new Counter()
ThreadA a = new ThreadA(c)
ThreadB b = new ThreadB(c)
Thread threadA = new Thread(a)
Thread threadB = new Thread(b)
threadA start()
threadB start()
}
}
执行上面的代码 可能的结果如下
before increment c =
before decrement c =
after increment c =
before increment c =
after decrement c =
before decrement c =
after increment c =
before increment c =
after decrement c =
before decrement c =
after increment c =
before increment c =
after decrement c =
before decrement c =
after increment c =
before increment c =
after decrement c =
before decrement c =
after increment c =
before increment c =
after decrement c =
before decrement c =
after increment c =
before increment c =
after increment c =
before increment c =
after decrement c =
before decrement c =
after decrement c =
before decrement c =
after increment c =
before increment c =
after increment c =
before increment c =
after decrement c =
before decrement c =
after increment c =
after decrement c =
before decrement c =
after decrement c =
从上面的输出结果中我们不难看出出现了严重的交错现象! 在increment或者是decrement方法中输出before和after本应该是成对连续出现的 但输出结果却不是如此
将上面代码的increment()和decrement()方法用synchronized 修饰后 重新运行该程序 输出结果如下
before increment c =
after increment c =
before increment c =
after increment c =
before decrement c =
after decrement c =
before increment c =
after increment c =
before decrement c =
after decrement c =
before decrement c =
after decrement c =
before decrement c =
after decrement c =
before decrement c =
after decrement c =
before increment c =
after increment c =
before decrement c =
after decrement c =
before decrement c =
after decrement c =
before decrement c =
after decrement c =
before decrement c =
after decrement c =
before increment c =
after increment c =
before decrement c =
after decrement c =
before increment c =
after increment c =
before increment c =
after increment c =
before increment c =
after increment c =
before increment c =
after increment c =
before increment c =
after increment c =
这样输出结果和没有增加synchronized修饰符时的大不相同 单独一次的increment和decrement方法并没有出现交错的现象 只是连续 次的increment()和decrement ()有交错(这个不是synchronized能解决的问题)
至少 我们从上面的实例程序中可以看到synchronized方法的作用了
lishixinzhi/Article/program/Java/hx/201311/26369
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)