我正在创建两个线程,并向它们传递一个函数,该函数执行下面显示的代码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)
这是输出:
如果两个对象返回相同的hashCode,是不是意味着它们是相等的?或者我们需要等于防止碰撞?
我可以通过比较hashCodes来实现equals吗?
我在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) 我正在开发俄语版的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 …
如何获得我的颜色并将其设置为不透明?
int myColor = getResources().getColor(R.color.ColorPrimary);
Run Code Online (Sandbox Code Playgroud)