小编Zha*_*bul的帖子

为什么这个多线程代码会在某些时候打印6?

我正在创建两个线程,并向它们传递一个函数,该函数执行下面显示的代码10,000,000次.

大多数情况下,"5"打印到控制台.有时它是"3"或"4".很清楚为什么会这样.

但是,它也打印"6".这怎么可能?

class Program
{
    private static int _state = 3;

    static void Main(string[] args)
    {
        Thread firstThread = new Thread(Tr);
        Thread secondThread = new Thread(Tr);

        firstThread.Start();
        secondThread.Start();

        firstThread.Join();
        secondThread.Join();

        Console.ReadLine();
    }

    private static void Tr()
    {
        for (int i = 0; i < 10000000; i++)
        {
            if (_state == 3)
            {
                _state++;
                if (_state != 4)
                {
                    Console.Write(_state);
                }
                _state = 3;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

在此输入图像描述

c# multithreading

32
推荐指数
2
解决办法
2550
查看次数

如果有hashCode(),为什么Java需要equals()?

如果两个对象返回相同的hashCode,是不是意味着它们是相等的?或者我们需要等于防止碰撞?

我可以通过比较hashCodes来实现equals吗?

java object

7
推荐指数
2
解决办法
691
查看次数

如何删除工具栏和RelativeLayout之间的分隔符

我在RelativeLayout下面android.support.v7.widget.Toolbar.它们都有相同的颜色.我怎样才能摆脱他们之间的陌生人? 在此输入图像描述 这是xml代码:

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/HomeColorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:id="@+id/spaceBelowToolbar"
        android:background="@color/HomeColorPrimary"
        android:orientation="vertical"
        android:elevation="4dp">
    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这是完整的xml文件:

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainBody"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/MainBG">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="@color/HomeColorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:layout_below="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:id="@+id/spaceBelowToolbar"
        android:background="@color/HomeColorPrimary"
        android:orientation="vertical"
        android:elevation="4dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="42dp"
            android:layout_alignParentLeft="true"
            android:id="@+id/MoneyValue"
            android:textColor="@color/md_white_1000"
            android:textSize="45sp"
            android:text="$850"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/MoneyValue"
            android:id="@+id/Balance"
            android:layout_marginLeft="42dp"
            android:textSize="25sp"
            android:textColor="@color/md_grey_300"
            android:text="BALANCE"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="180dp"
            android:id="@+id/PercentageValue"
            android:textColor="@color/md_white_1000"
            android:textSize="45sp" …
Run Code Online (Sandbox Code Playgroud)

java android toolbar

6
推荐指数
1
解决办法
2150
查看次数

用于在Android Studio中编码UTF-8的不可映射字符

我正在开发俄语版的Android应用程序.

这是我的AlertMessage班级,只有一个方法和一个俄语单词:

public class AlertMessage {

public static boolean isYes (Context context, String header, String message){
    final boolean[] isYes = {false};
    new AlertDialog.Builder(context)
            .setTitle(header)
            .setMessage(message)
            .setNegativeButton(android.R.string.no, null)
            .setPositiveButton("??",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface arg0, int arg1) {

                            isYes[0] =  true;

                        }
                    }).create().show();
    return isYes[0];
}
}
Run Code Online (Sandbox Code Playgroud)

我把它叫做MainActivity:

 @Override
public void onBackPressed(){

    if(AlertMessage.isYes(this,"?????","?? ???????, ??? ?????? ????? ?? ?????????"))
    {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        App.super.onBackPressed();
        Log.i("App", "Exit");
        finish();
    }
}
Run Code Online (Sandbox Code Playgroud)

一切都很好,除了AlertMessage …

java android utf-8 android-studio

2
推荐指数
1
解决办法
2596
查看次数

在Android中将不透明设置为自定义颜色(setAlpha)

如何获得我的颜色并将其设置为不透明?

int myColor = getResources().getColor(R.color.ColorPrimary);
Run Code Online (Sandbox Code Playgroud)

java android

1
推荐指数
1
解决办法
920
查看次数

标签 统计

java ×4

android ×3

android-studio ×1

c# ×1

multithreading ×1

object ×1

toolbar ×1

utf-8 ×1