Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. public class WorkerThread extends Thread {
  2. String operation = "";
  3. private Tuple incomingTuple;
  4. int result = 0;
  5.  
  6. @Override
  7. public void run() {
  8. while(true) {
  9. try {
  10. // Sleep for 100 milliseconds. This is just an example.
  11. // You can do something else instead of sleeping.
  12. Thread.sleep(100);
  13. } catch (Exception ex) {
  14. ;
  15. }
  16.  
  17. if (incomingTuple == null) {
  18. continue;
  19. }
  20.  
  21. if (operation.equalsIgnoreCase("add")) {
  22. result = incomingTuple.getInt("x") + incomingTuple.getInt("y");
  23. } else if (operation.equalsIgnoreCase("subtract")) {
  24. result = incomingTuple.getInt("x") - incomingTuple.getInt("y");
  25. } else if (operation.equalsIgnoreCase("multiply")) {
  26. result = incomingTuple.getInt("x") * incomingTuple.getInt("y");
  27. } else if (operation.equalsIgnoreCase("divide")) {
  28. result = incomingTuple.getInt("x") / incomingTuple.getInt("y");
  29. } else {
  30. System.out.println("Invalid operation.");
  31. continue;
  32. }
  33.  
  34. StreamingOutput<OutputTuple> outStream = getOutput(0);
  35. OutputTuple outTuple = outStream.newTuple();
  36.  
  37. outTuple.setInt("x", incomingTuple.getInt("x"));
  38. outTuple.setInt("y", incomingTuple.getInt("y"));
  39. outTuple.setInt("result", result);
  40. outTuple.setString("operation", operation);
  41.  
  42. try {
  43. outStream.submit(outTuple);
  44. } catch(Exception ex) {
  45. // Do something with this exception if you want to.
  46. }
  47.  
  48. incomingTuple = null;
  49. }
  50. }
  51.  
  52. void setOperation(String op) {
  53. operation = op;
  54. }
  55.  
  56. void setIncomingTuple(Tuple x) {
  57. incomingTuple = x;
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement