我一直在使用ConcurrentHashMap,如果我想实现以下目标.
ConcurrentModificationException,而另一个线程正在修改地图内容.有时,我enum用作密钥,从EnumMap Javadoc,我意识到,
集合视图返回的迭代器非常一致:它们永远不会抛出ConcurrentModificationException,它们可能会也可能不会显示迭代进行过程中对映射所做的任何修改的影响.
因此,我可以安全地更换
Map<Country, String> map = new ConcurrentHashMap<Country, String>();
Run Code Online (Sandbox Code Playgroud)
同
Map<Country, String> map = Collections.synchronizedMap(new EnumMap<Country, String>(Country.class));
Run Code Online (Sandbox Code Playgroud)
我知道有没有putIfAbsent在EnumMap,但这不要紧,我在这一刻,因为我并不需要它.
我有以下fun将由非事件派发线程执行.在线程中间,我想要一个
但是,我发现以线程安全方式执行它并不容易,因为对话框应该由事件调度线程显示.我试试
public int fun()
{
// The following code will be executed by non event dispatching thread.
final int choice;
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
// Error.
choice = JOptionPane.showConfirmDialog(SaveToCloudJDialog.this, message, title, JOptionPane.YES_NO_OPTION);
}
});
return choice;
}
Run Code Online (Sandbox Code Playgroud)
当然这不会choice是最终的,我不能将对话框的返回值分配给它.
实现上述3个目标的正确方法是什么?
通过参考http://www.javamex.com/tutorials/synchronization_volatile.shtml,由于附加规则3,我不确定volatile在下列情况下是否需要使用关键字.
线程A写入的新值是否会在"死"之后始终提交给主内存?如果是,是否意味着volatile如果符合上述3个条件我不需要关键字?
我怀疑volatile在这种情况下是否需要.根据需要,ArrayList可能会损坏.由于一个线程可以执行插入和更新size成员变量.后来,另一个线程(不同时地)可以读取ArrayList的size.如果查看ArrayList源代码,size则不会被声明为volatile.
在JavaDoc中ArrayList,只提到ArrayList用于多个线程同时访问ArrayList实例是不安全的,但是不能让多个线程在不同的时间访问ArrayList实例.
让我使用以下代码来解决此问题
public static void main(String[] args) throws InterruptedException {
// Create and start the thread
final ArrayList<String> list = new ArrayList<String>();
Thread writeThread = new Thread(new Runnable() {
public void run() {
list.add("hello");
}
});
writeThread.join();
Thread readThread = new Thread(new Runnable() …Run Code Online (Sandbox Code Playgroud) 回到NetBeans GUI构建器(Matisse)足够成熟的前一天,我需要执行手动编码,以生成Swing GUI表单/对话框布局.
现在,我不再需要为Swing GUI执行任何手动编码,因为Netbeans能够生成正确的Swing GUI代码. - 一个快乐的NetBeans用户:)
现在,对于Android App开发,我想知道,你们大多数人是在进行手工编码,还是使用某种WYSIWYG工具来生成布局XML文件?
直到现在,我已经尝试过Eclipse + SDK,DroidDraw.但是,我对这些工具感到非常失望,因为他们没有能够产生我想要的结果.
目前,我有一个应用程序,它将保存临时屏幕截图,并让用户通过Facebook共享它.图像保存代码或多或少如下.
// Use external cache dir, as our image file is large (>1M)
File outputDir = this.getExternalCacheDir();
File tempFile = File.createTempFile("CHEOK", "CHEOK", outputDir);
// In Desktop's J2SE, this file will automatically be deleted, once my desktop
// application quit. However, this is not the case for Android. My guess is, JVM
// is not terminated, even our Android application had quit.
tempFile.deleteOnExit();
Run Code Online (Sandbox Code Playgroud)
我意识到即使我退出我的Android应用程序(通过按下系统"后退"按钮),即使我们正在使用,tempFile仍然存在deleteOnExit.我的猜测是JVM还没有终止.
我可以知道,在Activity的期间,删除创建的临时文件是我们的应用程序员onDestroy吗?如果没有,常见的最佳做法是什么?
我从Android的Pair.java获取以下代码片段
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Pair)) return false;
final Pair<F, S> other;
try {
other = (Pair<F, S>) o;
} catch (ClassCastException e) {
return false;
}
return first.equals(other.first) && second.equals(other.second);
}
Run Code Online (Sandbox Code Playgroud)
我想知道,在instanceof返回true 之后,怎么可能有ClassCastException .
我尝试了以下代码进行应用内购买.我正在CurrentAppSimulator用于测试目的.
private async void history_Click(object sender, RoutedEventArgs e)
{
bool OK = true;
// Get the license info
// The next line is commented out for testing.
// licenseInformation = CurrentApp.LicenseInformation;
// The next line is commented out for production/release.
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
{
try
{
// The customer doesn't own this feature, so
// show the purchase dialog.
await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
// the in-app purchase was successful
OK = true;
}
catch (Exception)
{ …Run Code Online (Sandbox Code Playgroud) 我有以下项目结构.

我StockInfo.java很好.
package org.yccheok.jstock.engine;
import android.os.Parcel;
import android.os.Parcelable;
public class StockInfo implements Parcelable {
...
...
Run Code Online (Sandbox Code Playgroud)
package org.yccheok.jstock.engine;
parcelable StockInfo;
Run Code Online (Sandbox Code Playgroud)
package org.yccheok.jstock.engine;
interface StockInfoObserver {
void update(StockInfo stockInfo);
}
Run Code Online (Sandbox Code Playgroud)
package org.yccheok.jstock.engine;
interface AutoCompleteApi {
void handle(String string);
void attachStockInfoObserver(StockInfoObserver stockInfoObserver);
}
Run Code Online (Sandbox Code Playgroud)
然而,Eclipse抱怨StockInfoObserver.aidl(它确实抱怨AutoCompleteApi.aidl,因为它无法处理StockInfoObserver.aidl),
参数stockInfo(1)未知类型StockInfo
我试了一个小时,但仍然无法找到,为什么在援助中,StockInfo虽然我没有得到认可
StockInfo.aidlStockInfo.java任何的想法?
这是完整的错误.

注意,AutoCompleteApi.aidl非常依赖StockInfoObserver.aidl.这就是你会看到错误的原因.
我分享整个项目供您参考:https://www.dropbox.com/s/0k5pe75jolv5mtq/jstock-android.zip
我尝试在3个文本视图之间有2个分隔符.我用android:divider和android:showDividers.但是,没有显示垂直分隔线.我在想,有什么我错过了吗?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
android:divider="?android:attr/dividerVertical"
android:dividerPadding="12dip"
android:showDividers="middle" >
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="ABC" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="EFG" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="HIJ" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud) 以前,我有以下ScrollView和布局.它的滚动直到选定的视图可见代码工作.
<ScrollView>
<LinearLayout>
<Info View 1/>
<Info View 2/>
<Info View 3/>
</LinearLayout>
</ScrollView>
private void initScrollView() {
if (this.selectedInfoView == null) {
// Nothing to scroll.
return;
}
ScrollView scrollView = (ScrollView)this.getView().findViewById(R.id.scrollView);
Rect rect = new Rect();
Rect scrollViewRect = new Rect();
selectedInfoView.getHitRect(rect);
scrollView.getDrawingRect(scrollViewRect);
int dy = rect.bottom - scrollViewRect.bottom;
if (dy > 0) {
scrollView.scrollBy(0, dy);
}
}
Run Code Online (Sandbox Code Playgroud)
注意,getHitRect将坐标方向返回到父级的一级.所以,上面的代码将起作用.
但是,当涉及到稍微复杂的情况.上面的代码不再有效.
<ScrollView>
<LinearLayout 0>
<TextView/>
<LinearLayout 1>
<Info View 1/>
<Info View 2/> …Run Code Online (Sandbox Code Playgroud)