当有一个生产者和一个消费者时,我的代码很有效。但是如果我有不止一个消费者或生产者,他们就会得到相同的价值。(我已经看到很多答案,但是它们很复杂,有没有简单的解决方案)。
class QC {
int n;
boolean valueSet = false;
synchronized int get() {
if(!valueSet) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
synchronized void put(int n) {
if(valueSet) {
try {
wait();
} catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
}
生产者
class ProducerC implements Runnable {
QC q;
ProducerC(QC q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
while(true) {
q.put(i++);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
}
消费
class ConsumerC implements Runnable {
QC q;
ConsumerC(QC q) {
this.q = q;
new Thread(this, "Consumer").start();
}
@Override
public void run() {
while(true) {
q.get();
}
}
}
主要
public class CorrectPC {
public static void main(String[] args) {
QC q = new QC();
new ProducerC(q);
new ConsumerC(q);
new ProducerC(q);
System.out.println("Press Control-C to stop.");
}
}
答案 0 :(得分:0)
我将使用Java提供的并发集合之一作为我的生产者和消费者之间的共同联系点。您可以从https://docs.oracle.com/javase/tutorial/essential/concurrency/collections.html了解更多信息。
答案 1 :(得分:0)
试试这个。
package com.sourcey.materiallogindemo;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;
public class SignupActivity extends AppCompatActivity {
//URL to get JSON Array
//JSON Node Names
public String nurl;
private static final String TAG = "SignupActivity";
@Bind(R.id.input_name) EditText _nameText;
@Bind(R.id.input_email) EditText _emailText;
@Bind(R.id.input_tell) EditText _tellText;
@Bind(R.id.input_password) EditText _passwordText;
@Bind(R.id.btn_signup) Button _signupButton;
@Bind(R.id.link_login) TextView _loginLink;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
ButterKnife.bind(this);
_signupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signup();
}
});
_loginLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Finish the registration screen and return to the Login activity
finish();
}
});
}
public void signup() {
Log.d(TAG, "Signup");
if (!validate()) {
onSignupFailed();
return;
}
_signupButton.setEnabled(false);
final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
R.style.AppTheme_Dark_Dialog);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("در حال انجام عملیات...");
progressDialog.show();
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String tell = _tellText.getText().toString();
String password = _passwordText.getText().toString();
// TODO: Implement your own signup logic here.
nurl = "http://someurl/json/user.php?type=register&mobile="+tell+"&p="+password+"&email="+email+"&name="+name;
//new JSONParse().execute();
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
onSignupSuccess();
progressDialog.dismiss();
}
}, 3000);
}
public void onSignupSuccess() {
_signupButton.setEnabled(true);
setResult(RESULT_OK, null);
finish();
}
public void onSignupFailed() {
Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
_signupButton.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
String name = _nameText.getText().toString();
String email = _emailText.getText().toString();
String tell = _tellText.getText().toString();
String password = _passwordText.getText().toString();
if (name.isEmpty() || name.length() < 3) {
_nameText.setError("at least 3 characters");
valid = false;
} else {
_nameText.setError(null);
}
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
_emailText.setError("enter a valid email address");
valid = false;
} else {
_emailText.setError(null);
}
if (tell.isEmpty() || !android.util.Patterns.PHONE.matcher(tell).matches()) {
_tellText.setError("enter a valid phone number");
valid = false;
} else {
_tellText.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
_passwordText.setError("between 4 and 10 alphanumeric characters");
valid = false;
} else {
_passwordText.setError(null);
}
return valid;
}
}