gpt4 book ai didi

java - 如何将我的主线程与创建的工作线程连接?

转载 作者:行者123 更新时间:2023-11-30 06:15:52 25 4
gpt4 key购买 nike

嘿,我在 android studio java 上使用房间数据库时遇到了麻烦。当我单击保存按钮时,我试图保存类的组件。但问题是,在主线程上使用保存或下载调用是一件坏事,因此我正在创建自己的线程,但如何在该线程之外使用数据?我正在做这个的类(class)看起来像这样......

更新:那么这应该有效吗?

package com.example.jenso.paperseller;

import android.arch.persistence.room.Room;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CreateCustomer extends AppCompatActivity {
EditText name;
EditText address;
EditText phonenumber;
EditText email;
Button saveCustomer;
PapperSellerDatabase pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_customer);
name = findViewById(R.id.nameInput);
address = findViewById(R.id.addressInput);
phonenumber = findViewById(R.id.phonenumberInput);
email = findViewById(R.id.emailInput);
saveCustomer = findViewById(R.id.saveInput);
setTheDataBase();


saveCustomer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(CreateCustomer.this, MainActivity.class));
pd.customerDao().insertAll(new Customer(name.getText().toString(),address.getText().toString(),phonenumber.getText().toString(),email.getText().toString()));
}
});




}
public void setTheDataBase(){
new Thread(new Runnable() {
@Override
public void run() {
//here you obtain data from async source
PapperSellerDatabase pd = Room.databaseBuilder(getApplicationContext(),PapperSellerDatabase.class,"production")
.fallbackToDestructiveMigration()
.build();


}
}).start();



}
}

在负载端我这样做吗?

package com.example.jenso.paperseller;

import android.arch.persistence.room.Room;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.ThemedSpinnerAdapter;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
List<Customer> customers;
PapperSellerDatabase database;


FloatingActionButton fab;
private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

recyclerView = findViewById(R.id.recycler);
customers = new ArrayList<>();


recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new PapperRecyclerAdapter(customers,this);
recyclerView.setAdapter(adapter);

downloadTheData();

fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: Do this when you click");
startActivity(new Intent(MainActivity.this, CreateCustomer.class));
}

});



}
private void downloadTheData(){
new Thread(new Runnable() {
@Override
public void run() {
//here you obtain data from async source
PapperSellerDatabase pd = Room.databaseBuilder(getApplicationContext(),PapperSellerDatabase.class,"production")
.fallbackToDestructiveMigration()
.build();


customers = pd.customerDao().getAllCustomers();

}
}).start();




}




}

那么,如果我单击想要将数据保存到房间数据库然后离开 View 的按钮,我该如何进一步操作呢?

最佳答案

每当您使用前提时,例如在 JavaScript 中,您都不确定前提何时会得到满足,或者换句话说,您的约会何时可用。因此,通常要做的就是在更高的作用域中声明变量,这将使该变量既可供应设置它的线程(工作线程)使用,也可供应使用它的线程(主线程)使用。

更新

public class Example {
//here you initialize your variable that will contain data
List <RemoteData> remoteData = null;

....

public void hereRemoteDataIsUsed(){
if(remoteData != null){
//use remoteData here
...
}
}

....
public void hereRemoteDataIsSet(){
new Thread(new Runnable() {
@Override
public void run() {
//here you obtain data from async source
remoteData = getDataFromSomeAsyncSource();
}
}).start();
}
}

您可以注意到,在获取数据的方法中没有返回任何内容。由于它是一个异步操作,您不知道它何时会完成。你只知道最终会如此。因此,您只需等待它可用 (remoteData != null)。

关于java - 如何将我的主线程与创建的工作线程连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com